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

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


当前回答

cell.separatorInset = UIEdgeInsetsMake(0.0, cell.bounds.size.width, 0.0, -cell.bounds.size.width)

在iOS 10.2中运行良好

其他回答

在willdisplaycell:

cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0)

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

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)
}

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

- (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];
}

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

在你的UITableViewCell子类中,覆盖layoutSubviews并隐藏_UITableViewCellSeparatorView。适用于iOS 10。

override func layoutSubviews() {
    super.layoutSubviews()

    subviews.forEach { (view) in
        if view.dynamicType.description() == "_UITableViewCellSeparatorView" {
            view.hidden = true
        }
    }
}