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

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


当前回答

对于那些专门寻找隐藏分隔线的尤里卡行,这是唯一的解决方案,为我工作:

     row.cellUpdate { (cell, row) in
            cell.separatorInset =  UIEdgeInsets(top: 0, left: 0, bottom: 0, right: CGFloat.greatestFiniteMagnitude)
        }

其他回答

我不相信这种方法在任何情况下都适用于动态单元格……

if (indexPath.row == self.newCarArray.count-1) {
  cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.f);
}

不管你在哪个tableview方法中为动态单元格做这件事,你改变了inset属性的单元格总是有inset属性设置,现在每次它被退出队列时,都会导致缺少行分隔符的狂暴…除非你自己改变。

这样的方法对我很有效:

if indexPath.row == franchises.count - 1 {
  cell.separatorInset = UIEdgeInsetsMake(0, cell.contentView.bounds.width, 0, 0)
} else {
  cell.separatorInset = UIEdgeInsetsMake(0, 0, cell.contentView.bounds.width, 0)
}

这样你就可以在每次加载时更新数据结构状态

在使用iOS 8.4的Swift中:

/*
    Tells the delegate that the table view is about to draw a cell for a particular row. (optional)
*/
override func tableView(tableView: UITableView,
                        willDisplayCell cell: UITableViewCell,
                        forRowAtIndexPath indexPath: NSIndexPath)
{
    if indexPath.row == 3 {
        // Hiding separator line for only one specific UITableViewCell
        cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)
    }
}

Note: this snippet above will work on UITableView using dynamic cells. The only problem that you can encounter is when you use static cells with categories, a separator type different than none and a grouped style for the table view. In fact, in this particular case it will not hide the last cell of each category. For overcoming that, the solution that I found was to set the cell separator (through IB) to none and then creating and adding manually (through code) your line view to each cell. For an example, please check the snippet below:

/*
Tells the delegate that the table view is about to draw a cell for a particular row. (optional)
*/
override func tableView(tableView: UITableView,
    willDisplayCell cell: UITableViewCell,
    forRowAtIndexPath indexPath: NSIndexPath)
{
    // Row 2 at Section 2
    if indexPath.row == 1 && indexPath.section == 1 {
        // Hiding separator line for one specific UITableViewCell
        cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)

        // Here we add a line at the bottom of the cell (e.g. here at the second row of the second section).
        let additionalSeparatorThickness = CGFloat(1)
        let additionalSeparator = UIView(frame: CGRectMake(0,
            cell.frame.size.height - additionalSeparatorThickness,
            cell.frame.size.width,
            additionalSeparatorThickness))
        additionalSeparator.backgroundColor = UIColor.redColor()
        cell.addSubview(additionalSeparator)
    }
}

在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横屏模式下,你不能保证你的分隔符总是不可见。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}

更简单、更符合逻辑的做法是:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [[UIView alloc] initWithFrame:CGRectZero];
}

在大多数情况下,您不希望只看到最后一个表视图单元格分隔符。这种方法只删除了最后一个表格视图单元格分隔符,你不需要考虑自动布局问题(即旋转设备)或硬编码值来设置分隔符嵌入。