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

如何删除UITableViewCell分隔符边距?


当前回答

这是在Swift中为我工作的代码:

override func viewDidLoad() 
{
    super.viewDidLoad()
    ...
    if tableView.respondsToSelector("setSeparatorInset:") {
        tableView.separatorInset = UIEdgeInsetsZero
    }
}

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath)
{
    if cell.respondsToSelector("setSeparatorInset:") {
        cell.separatorInset.left = CGFloat(0.0)
    }
    if tableView.respondsToSelector("setLayoutMargins:") {
        tableView.layoutMargins = UIEdgeInsetsZero
    }
    if cell.respondsToSelector("setLayoutMargins:") {
        cell.layoutMargins.left = CGFloat(0.0)
    }
}

这对我来说似乎是最干净的(目前),因为所有的单元格/tableView边缘/边缘调整都是在tableView:willDisplayCell:forRowAtIndexPath:方法中完成的,而不是在tableView:cellForRowAtIndexPath:中填充不必要的代码。

顺便说一句,我只是设置单元格的左separatorInset/ layoutmargin,因为在这种情况下,我不想搞砸我在单元格中设置的约束。

代码更新到Swift 2.2:

 override func viewDidLoad() {
   super.viewDidLoad()       

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

 override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath) {
        if cell.respondsToSelector(Selector("setSeparatorInset:")) {
            cell.separatorInset.left = CGFloat(0.0)
        }
        if tableView.respondsToSelector(Selector("setLayoutMargins:")) {
            tableView.layoutMargins = UIEdgeInsetsZero
        }
        if cell.respondsToSelector(Selector("setLayoutMargins:")) {
            cell.layoutMargins.left = CGFloat(0.0)
        }
    }

其他回答

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

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

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

这就是我的解。这适用于自定义单元格子类,只需将它们都添加到子类。

- (UIEdgeInset)layoutMargins { 返回 UIEdgeInsetsMake(0, 10, 0, 10); }

2.

self.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);

而且很方便,你可以自定义分隔符的位置,而不需要你的设计师为你画一个..........

这是在Swift中为我工作的代码:

override func viewDidLoad() 
{
    super.viewDidLoad()
    ...
    if tableView.respondsToSelector("setSeparatorInset:") {
        tableView.separatorInset = UIEdgeInsetsZero
    }
}

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath)
{
    if cell.respondsToSelector("setSeparatorInset:") {
        cell.separatorInset.left = CGFloat(0.0)
    }
    if tableView.respondsToSelector("setLayoutMargins:") {
        tableView.layoutMargins = UIEdgeInsetsZero
    }
    if cell.respondsToSelector("setLayoutMargins:") {
        cell.layoutMargins.left = CGFloat(0.0)
    }
}

这对我来说似乎是最干净的(目前),因为所有的单元格/tableView边缘/边缘调整都是在tableView:willDisplayCell:forRowAtIndexPath:方法中完成的,而不是在tableView:cellForRowAtIndexPath:中填充不必要的代码。

顺便说一句,我只是设置单元格的左separatorInset/ layoutmargin,因为在这种情况下,我不想搞砸我在单元格中设置的约束。

代码更新到Swift 2.2:

 override func viewDidLoad() {
   super.viewDidLoad()       

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

 override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,forRowAtIndexPath indexPath: NSIndexPath) {
        if cell.respondsToSelector(Selector("setSeparatorInset:")) {
            cell.separatorInset.left = CGFloat(0.0)
        }
        if tableView.respondsToSelector(Selector("setLayoutMargins:")) {
            tableView.layoutMargins = UIEdgeInsetsZero
        }
        if cell.respondsToSelector(Selector("setLayoutMargins:")) {
            cell.layoutMargins.left = CGFloat(0.0)
        }
    }

经过大量调查……

这是我能找到的完全控制这些东西的唯一方法

完全控制每个单元格上的分隔符插入和布局边距。在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