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

如何删除UITableViewCell分隔符边距?


当前回答

我相信这是我在这里问的相同的问题:为XCode 6 iPhone模拟器删除iOS 8 UITableView上的SeparatorInset

在iOS 8中,所有继承自UIView的对象都有一个新属性。在ios7中设置SeparatorInset的解决方案。x将不能删除你在iOS 8中UITableView上看到的空白。

新的属性叫做“layoutmargin”。

@property(nonatomic) UIEdgeInsets layoutMargins
Description   The default spacing to use when laying out content in the view.
Availability  iOS (8.0 and later)
Declared In   UIView.h
Reference UIView Class Reference

解决方案:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }

   if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
   }
}

如果你设置单元格。layoutmargin = UIEdgeInsetsZero;如果不检查layoutmargin是否存在,应用程序将在iOS 7.x上崩溃。所以,最好的方法是在setlayoutmargin:UIEdgeInsetsZero之前检查layoutmargin是否存在。

其他回答

使用Swift 2.2

创建UITableViewCell扩展

import UIKit

extension UITableViewCell {
    func removeMargins() {

        if self.respondsToSelector(Selector("setSeparatorInset:")) {
            self.separatorInset = UIEdgeInsetsZero
        }

        if self.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")) {
            self.preservesSuperviewLayoutMargins = false
        }

        if self.respondsToSelector(Selector("setLayoutMargins:")) {
            self.layoutMargins = UIEdgeInsetsZero
        }
    }
} 

现在可以在cellForRowAtIndex中使用

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
            cell.removeMargins()//To remove seprator inset 
}

我相信这是我在这里问的相同的问题:为XCode 6 iPhone模拟器删除iOS 8 UITableView上的SeparatorInset

在iOS 8中,所有继承自UIView的对象都有一个新属性。在ios7中设置SeparatorInset的解决方案。x将不能删除你在iOS 8中UITableView上看到的空白。

新的属性叫做“layoutmargin”。

@property(nonatomic) UIEdgeInsets layoutMargins
Description   The default spacing to use when laying out content in the view.
Availability  iOS (8.0 and later)
Declared In   UIView.h
Reference UIView Class Reference

解决方案:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }

   if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
   }
}

如果你设置单元格。layoutmargin = UIEdgeInsetsZero;如果不检查layoutmargin是否存在,应用程序将在iOS 7.x上崩溃。所以,最好的方法是在setlayoutmargin:UIEdgeInsetsZero之前检查layoutmargin是否存在。

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

在调试器中快速浏览会告诉你分隔行是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!)

使用下面的代码片段避免不必要的填充问题UITableView在IOS 8和7。

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    if ([tableView respondsToSelector:@selector(setSeparatorInset:)])
    {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([tableView respondsToSelector:@selector(setLayoutMargins:)])
    {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

我是这样做的:

tableView.separatorInset = UIEdgeInsetsZero;
tableView.layoutMargins = UIEdgeInsetsZero;
cell.layoutMargins = UIEdgeInsetsZero;