我在定制一个UITableView。我想隐藏在最后一个单元格上的分离线…我能这样做吗?

我知道我可以用tableView。separatorStyle = UITableViewCellStyle。没有,但是这会影响tableView的所有单元格。我希望它只影响最后一个单元格。


当前回答

设置separatorInset。right = . greatestfinitemagnitude。

其他回答

在Swift 3, Swift 4和Swift 5中,你可以像这样写一个UITableViewCell扩展:

extension UITableViewCell {
  func separator(hide: Bool) {
    separatorInset.left = hide ? bounds.size.width : 0
  }
}

然后你可以这样使用它(当cell是你的cell实例时):

cell.separator(hide: false) // Shows separator 
cell.separator(hide: true) // Hides separator

将表格视图单元格的宽度赋为左插入值比赋给它一些随机数更好。因为在某些屏幕尺寸中,也许不是现在,但将来你的分隔符仍然是可见的因为这个随机数可能不够。此外,在iPad横屏模式下,你不能保证你的分隔符总是不可见。

在viewDidLoad中添加这一行:

self.tableView.separatorColor = [UIColor clearColor];

在cellForRowAtIndexPath中:

适用于iOS低版本

if(indexPath.row != self.newCarArray.count-1){
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, 320, 2)];
    line.backgroundColor = [UIColor redColor];
    [cell addSubview:line];
}

适用于iOS 7以上版本(包括iOS 8)

if (indexPath.row == self.newCarArray.count-1) {
    cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.f);
}
  if([_data count] == 0 ){
       [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];//  [self tableView].=YES;
    } else {
      [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];////    [self tableView].hidden=NO;
    }

试试下面的代码,也许能帮你解决问题

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

   NSString* reuseIdentifier = @"Contact Cell";

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
if (indexPath.row != 10) {//Specify the cell number
        cell.backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgWithLine.png"]];

} else {
        cell.backgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bgWithOutLine.png"]];

}

    }

    return cell;
}

设置separatorInset。right = . greatestfinitemagnitude。