我有一个应用程序,其中UITableView的分隔符插入设置为自定义值-右0,左0。这在iOS 7中非常有效。x,但是在iOS 8.0中,我看到右边的分隔符被设置为默认值15。即使在xib文件中它设置为0,它仍然不正确地显示。
如何删除UITableViewCell分隔符边距?
我有一个应用程序,其中UITableView的分隔符插入设置为自定义值-右0,左0。这在iOS 7中非常有效。x,但是在iOS 8.0中,我看到右边的分隔符被设置为默认值15。即使在xib文件中它设置为0,它仍然不正确地显示。
如何删除UITableViewCell分隔符边距?
当前回答
经过大量调查……
这是我能找到的完全控制这些东西的唯一方法
完全控制每个单元格上的分隔符插入和布局边距。在UITableviewDelegate的willDisplayCell方法中做到这一点。
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.layoutMargins = UIEdgeInsetsZero
cell.contentView.layoutMargins = UIEdgeInsetsMake(0, 10, 0, 10)
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
}
cell对象控制分隔符,contentView控制其他一切。如果你的分隔符插入空间显示在一个意想不到的颜色,这应该解决它:
cell.backgroundColor = cell.contentView.backgroundColor
其他回答
大多数答案是显示分隔符插入和布局边距设置在各种方法(即viewDidLayoutSubviews, willDisplayCell等)的单元格和表视图,但我发现,只是把这些cellForRowAtIndexPath工作得很好。似乎是最干净的方法。
// kill insets for iOS 8
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8) {
cell.preservesSuperviewLayoutMargins = NO;
[cell setLayoutMargins:UIEdgeInsetsZero];
}
// iOS 7 and later
if ([cell respondsToSelector:@selector(setSeparatorInset:)])
[cell setSeparatorInset:UIEdgeInsetsZero];
对我来说,简单的线条就够了
cell.layoutMargins = UIEdgeInsetsZero
在iOS8:
添加这个到我的UITableViewCell子类:
- (UIEdgeInsets)layoutMargins {
return UIEdgeInsetsZero;
}
和这个到"tableView:cellForRowAtIndexPath"或"tableView:willDisplayCell":
[editCell setSeparatorInset:UIEdgeInsetsZero];
对我有用。
以上的解决方案我都没有什么运气。我正在为我的表格单元格使用NIB文件。我通过添加一个高度为1的标签来“修复”这个问题。我把标签的背景改为黑色,把标签钉在笔尖的底部,然后把剩下的内容钉在添加的标签上。现在我在单元格的底部有了一条黑色边框。
对我来说,这感觉更像是一种黑客,但它确实有效。
我唯一的选择就是完全消除边界。我还没决定要不要这么做。
迅速:
override func viewDidLoad() {
super.viewDidLoad()
if self.tableView.respondsToSelector("setSeparatorInset:") {
self.tableView.separatorInset = UIEdgeInsetsZero
}
if self.tableView.respondsToSelector("setLayoutMargins:") {
self.tableView.layoutMargins = UIEdgeInsetsZero
}
self.tableView.layoutIfNeeded() // <--- this do the magic
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
...
if cell.respondsToSelector("setSeparatorInset:") {
cell.separatorInset = UIEdgeInsetsZero
}
if cell.respondsToSelector("setLayoutMargins:") {
cell.layoutMargins = UIEdgeInsetsZero
}
return cell
}