如何在静态单元格中隐藏单元格分隔符?

时间:2015-07-15 06:34:57

标签: ios uitableview

我在UITableView中有自定义静态单元格,我想删除某个指定单元格中的单元格分隔符。但是我如何在静态单元格中以编程方式执行此操作?

我的设备:iphone 5 iOS 7 IOS模拟器:iphone 4s-iphone 6p,全部是iOS 8.3

4 个答案:

答案 0 :(得分:2)

您最好的选择可能是将表格的separatorStyle设置为UITableViewCellSeparatorStyleNone,并在需要时手动添加/绘制一条线(可能在tableView:cellForRowAtIndexPath :)。

像这样使用,

...
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

    // Drawing our own separatorLine here because I need to turn it off for the
    // last row. I can only do that on the tableView and on on specific cells.
    // The y position below has to be 1 less than the cell height to keep it from
    // disappearing when the tableView is scrolled.
    UIImageView *separatorLine = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, cell.frame.size.height - 1.0f, cell.frame.size.width, 1.0f)];
    separatorLine.image = [[UIImage imageNamed:@"grayDot"] stretchableImageWithLeftCapWidth:1 topCapHeight:0];
    separatorLine.tag = 4;

    [cell.contentView addSubview:separatorLine];

    [separatorLine release];
}

// Setup default cell setttings.
...
UIImageView *separatorLine = (UIImageView *)[cell viewWithTag:4];
separatorLine.hidden = NO;
...
// In the cell I want to hide the line, I just hide it.
seperatorLine.hidden = YES;

在ViewDidload中设置

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 

答案 1 :(得分:1)

只需在 - cellForRowAtIndexPath:方法

中使用此代码即可
 cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.f);

如果您想要特定的单元格: -

if(indexpath.row==1) //desired row number on which you don't want separator
{
 cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.f);
} 

答案 2 :(得分:0)

由于分隔符样式是表的属性,而不是单元格,因此最简单的方法是隐藏所有分隔符:

tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

然后在需要一个的单元格中明确绘制一个分隔符。通常,最简单的方法是在单元格的底部(或顶部)添加一个像素高的子视图,并将其背景颜色设置为黑色。

答案 3 :(得分:0)

建立从静态单元格到UITableViewController的链接:

- (void)viewDidLoad {
    [super viewDidLoad];
    yourStaticCell.separatorInset = UIEdgeInsetsMake(0.f, yourStaticCell.bounds.size.width, 0.f, 0.f);
}