我有一个应用程序,其中UITableView的分隔符插入设置为自定义值-右0,左0。这在iOS 7中非常有效。x,但是在iOS 8.0中,我看到右边的分隔符被设置为默认值15。即使在xib文件中它设置为0,它仍然不正确地显示。

如何删除UITableViewCell分隔符边距?


当前回答

对于iOS 9,你需要添加:

if([myTableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)])
{
    myTableView.cellLayoutMarginsFollowReadableWidth = NO;
} 

详情请参阅问题。

其他回答

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        // ... Get the cell
        cell.separatorInset = UIEdgeInsetsMake(0.f, 20.f, 0.f, [UIScreen mainScreen].bounds.size.width - 20);
        // others
        return cell;
}

对于要隐藏分隔符的任何特定单元格。

在盲目地试图解决问题之前,让我们花点时间来了解一下这个问题。

在调试器中快速浏览会告诉你分隔行是UITableViewCell的子视图。似乎单元格本身对这些线条的布局负有相当大的责任。

iOS 8引入了布局边距的概念。默认情况下,视图的布局边距为8pt,它们继承自祖先视图。

正如我们所能告诉的,当布局它的分隔线时,UITableViewCell选择尊重左边的布局边距,使用它来约束左边的插图。

把所有这些放在一起,为了实现真正为零的理想插入,我们需要:

将左侧布局边距设置为0 停止任何继承的边缘覆盖它

这样说来,这是一个相当简单的任务:

cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;

注意事项:

This code only needs to be run once per cell (you're just configuring the cell's properties after all), and there's nothing special about when you choose to execute it. Do what seems cleanest to you. Sadly neither property is available to configure in Interface Builder, but you can specify a user-defined runtime attribute for preservesSuperviewLayoutMargins if desired. Clearly, if your app targets earlier OS releases too, you'll need to avoid executing the above code until running on iOS 8 and above. Rather than setting preservesSuperviewLayoutMargins, you can configure ancestor views (such as the table) to have 0 left margin too, but this seems inherently more error-prone as you don't control that entire hierarchy. It would probably be slightly cleaner to set only the left margin to 0 and leave the others be. If you want to have a 0 inset on the "extra" separators that UITableView draws at the bottom of plain style tables, I'm guessing that will require specifying the same settings at the table level too (haven't tried this one!)

大多数答案是显示分隔符插入和布局边距设置在各种方法(即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];

简单的解决方案在Swift为iOS 8自定义UITableViewCell

override func awakeFromNib() {
    super.awakeFromNib()

    self.layoutMargins = UIEdgeInsetsZero
    self.separatorInset = UIEdgeInsetsZero
}

通过这种方式,你只设置一次layoutMargin和separatorInset,而不是像上面大多数答案所建议的那样对每个willDisplayCell都进行设置。

如果你在使用自定义UITableViewCell,这是正确的地方。 否则你应该用tableView:cellForRowAtIndexPath。

只是另一个提示:你不需要设置preservessuperviewlayoutmargin = false,因为默认值已经是NO!

Arg ! !在你的Cell子类中做这些事情后:

- (UIEdgeInsets)layoutMargins
{
    return UIEdgeInsetsZero;
}

或者设置单元格。layoutmargin = UIEdgeInsetsZero;帮我修好了。