我有一个分组UITableview,它是通过编程创建的。此外,我还以编程方式在tableview中填充了一个带有xib文件的单元格。到目前一切尚好。但我只想移除外分隔线。我使用了下面的代码,但这次删除了所有分隔线。
self.tableView.separatorColor = UIColor clearColor;
对我的情况来说,这不是个好选择。这是我想做的截图;
发布于 2018-02-01 03:37:39
甚至在2018年,谷歌( Google )也把这个页面作为这个问题的最高结果。在iOS 11中,我没有得到任何提供的答案,所以我想出了以下内容:
extension UITableViewCell {
func removeSectionSeparators() {
for subview in subviews {
if subview != contentView && subview.frame.width == frame.width {
subview.removeFromSuperview()
}
}
}
}
现在,在任何.removeSectionSeparators实例上调用UITableViewCell ()都可以解决这个问题。至少在我的例子中,区段分隔符是唯一与单元格本身具有相同宽度的分隔符(其他部分都是缩进的)。
剩下的唯一问题是我们应该把它叫做什么地方。您可能认为willDisplayCell是最好的选择,但我发现初始调用发生在分隔符视图本身生成之前,所以没有骰子。
最后,在返回一个重新加载的单元格之前,我将它放在我的cellForRowAtIndexPath方法中:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyReusableIdentifier", for: indexPath)
Timer.scheduledTimer(withTimeInterval: 0.15, repeats: false) { (timer) in
cell.removeSectionSeparators()
}
return cell
}
它没有那么优雅,但我还没有遇到任何问题。
编辑:看起来我们也需要这个(对于重用的单元格):
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.removeSectionSeparators()
}
下面是屏幕截图中的前/后代码:
先于
后
https://stackoverflow.com/questions/29006311
复制相似问题