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

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


当前回答

当我使用扩展和调用layoutSubviews()立即更新布局视图时,它为我工作。

extension UITableViewCell {

    func removeSeparator() {
        separatorInset = UIEdgeInsetsMake(0, bounds.size.width, 0, 0)
    }
}

override func layoutSubviews() {
    super.layoutSubviews()

    removeSeparator()
}

其他回答

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

我用这个解出来了

override func layoutSubviews() {
    super.layoutSubviews()

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

   tableView.separatorStyle = UITableViewCellSeparatorStyle.None 

}

当我使用扩展和调用layoutSubviews()立即更新布局视图时,它为我工作。

extension UITableViewCell {

    func removeSeparator() {
        separatorInset = UIEdgeInsetsMake(0, bounds.size.width, 0, 0)
    }
}

override func layoutSubviews() {
    super.layoutSubviews()

    removeSeparator()
}

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

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

太棒了;)

我的要求是将分隔符隐藏在第4和第5单元格之间。我通过

    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row == 3)
    {
        cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0);
    }
}