xcode IOS UITableViewCell Spacer(或边距)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11431009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:51:05  来源:igfitidea点击:

IOS UITableViewCell Spacer (or margin)

objective-ciosxcodeuitableview

提问by Nick Peachey

I'm trying to get a space between my table custom view cells on my table view controller. For example, for each cell created it is stacked up one by one and there is no margin. For example on the facebook iPhone app has a spacer after each cell which i would like to create, any ideas guys?

我试图在我的表格视图控制器上的表格自定义视图单元格之间留出一个空间。例如,对于创建的每个单元格,它都被一个一个地堆叠起来,并且没有边距。例如,在 facebook iPhone 应用程序上,我想创建的每个单元格后面都有一个间隔符,有什么想法吗?



Edit: From comment below

编辑:来自下面的评论

The code looks like this

代码看起来像这样

MSRSalesCompanyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
cell.backgroundView = [[CustomCellBackground alloc] init]; 
NSDictionary *company = [companies objectAtIndex:[indexPath row]]; 
cell.companyName.text = [company objectForKey:@"Name"]; 
cell.backgroundColor = [UIColor clearColor]; 
cell.backgroundView.bounds = CGRectMake(0, 0, 320, 55); 
[self loadImageAsync:cell withImageUrl:imageURL];

回答by A-Live

Check my answerfor the similar question, it suggests you to create the invisible cells between the cells you want to divide with some space.

检查我的答案了类似的问题,它建议你创建你想要一些空间来划分细胞之间的无形的细胞。

Option 2 is to create only the visible cells, make their height above the visible area and prepare special contentand selectedview's, note the selected view creation is not so easy and you'll need to do it as you probably don't want the spacing area to get selected, so i'm using the first option when there's a need to get some cell's spacing.

选项 2 是仅创建可见单元格,使它们的高度高于可见区域并准备特殊contentselected视图,注意所选视图的创建不是那么容易,您需要这样做,因为您可能不想要间隔区域被选中,所以当需要获得一些单元格的间距时,我使用第一个选项。

The main (and probably the only) disadvantage of the first option is that you have to treat the cell's indexes in a special way to distinguish the spacing-cells and the visible-cells.

第一个选项的主要(也可能是唯一的)缺点是您必须以特殊方式处理单元格的索引以区分间隔单元格和可见单元格。

回答by WaaleedKhan

Okayy i did some thing like this.....

好吧,我做了这样的事情......

First the BGColor Of view which will hold the Tableview is set to White (it was a personal choice w.r.t to my design) and then the cellForRowAtIndexPathmethod looks some thing like this.

首先,将保存 Tableview 的 BGColor Of 视图设置为白色(这是我的设计的个人选择),然后该cellForRowAtIndexPath方法看起来像这样。

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell;
    cell = nil;

    //Create Cell
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];    
    }

    //Retrive Values From DB
    DBTable_Description *dbObject = [[DBTable_Description alloc]init];
    dbObject = [contentList objectAtIndex:[indexPath row]];
    //Cell View
    UIView *cellView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 65)];
    //ImageView
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(4.0, 8.0, 300.0, 58.0)];
    imageView.userInteractionEnabled = NO;
    imageView.image = imgForCellBkg;
    //Label
    UILabel *lblFor = [[UILabel alloc]initWithFrame:CGRectMake(70, 25, 200, 21)];
    lblFor.text =dbObject.field1;
    lblFor.backgroundColor = [UIColor clearColor];
    lblFor.font = [UIFont fontWithName:@"Helvetica Neue" size:16];
    lblFor.textColor = [UIColor grayColor];
    lblFor.shadowColor = [UIColor grayColor];

    //Adding Views to Cell View
    [cellView addSubview:imageView];
    [cellView addSubview:lblFor];

    [cell.contentView addSubview:cellView];



    cell.selectionStyle = NO;
    return cell;


}

Okay First and formost neglect the Database code. Now what i did is i created a View on each Cell name cellView(set the height as per ur requirement) next i had an Image View and set the appropriate image (again it may not be the thing u want to do) but pay attention to the CGRectMake i se the values according to the amount of gap i want b.w my cells.. the rest is usual.

好的 首先忽略数据库代码。现在我所做的是我在每个单元格名称上创建了一个视图cellView(根据您的要求设置高度)接下来我有一个图像视图并设置了适当的图像(这可能不是您想要做的事情)但要注意CGRectMake 我根据我想要 bw 我的单元格的间隙量设置值.. 其余的都是平常的。

here is the image of the view i had in my AppSee What i meant....???

这是我在我的应用程序中的视图图像看到我的意思......?

Let me Know if it worked Cheers W

让我知道它是否有效干杯 W

回答by Ashley Mills

If your tableView's rowHeight is 50 points, make your background image view height say, 40 points, and centre it in the cell. Then make your cell background colour [UIcolor clearColor]

如果您的 tableView 的 rowHeight 为 50 点,则将背景图像视图高度设为 40 点,并将其居中放置在单元格中。然后让你的单元格背景颜色[UIcolor clearColor]