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

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


当前回答

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

       NSString *cellId = @"cell";
       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
       NSInteger lastRowIndexInSection = [tableView numberOfRowsInSection:indexPath.section] - 1;

       if (row == lastRowIndexInSection) {
              CGFloat halfWidthOfCell = cell.frame.size.width / 2;
              cell.separatorInset = UIEdgeInsetsMake(0, halfWidthOfCell, 0, halfWidthOfCell);
       }
}

其他回答

如果接受的答案不成立,你可以试试这个:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f; }

太棒了;)

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

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

在iOS9上,我有一个问题,改变分隔符插入也会影响文本和细节标签的定位。

我用这个解出来了

override func layoutSubviews() {
    super.layoutSubviews()

    separatorInset = UIEdgeInsets(top: 0, left: layoutMargins.left, bottom: 0, right: width - layoutMargins.left)
}

长筒5 . 13+

当你定义你的表时,只需添加:

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    // Removes separator lines
    tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
    return UIView()
}

神奇的一行是tableView。separatorStyle = UITableViewCell.SeparatorStyle.none

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