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

如何删除UITableViewCell分隔符边距?


当前回答

这在我的iOS 8和iOS 9系统中运行得非常好。

对于OBJ-C

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(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];
        }
         return cell;
    }

其他回答

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

- (UIEdgeInsets)layoutMargins
{
    return UIEdgeInsetsZero;
}

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

简单地把这两行放在cellForRowAtIndexPath方法中

如果你想要所有的分隔线都从0开始 (细胞setSeparatorInset: UIEdgeInsetsZero); (细胞setLayoutMargins: UIEdgeInsetsZero);

如果你想特定的分隔线是从0开始假设这是最后一行是从0开始

if (indexPath.row == array.count-1) 
{
   [cell setSeparatorInset:UIEdgeInsetsZero];
   [cell setLayoutMargins:UIEdgeInsetsZero];
}
else
   tblView.separatorInset=UIEdgeInsetsMake(0, 10, 0, 0);

这在我的iOS 8和iOS 9系统中运行得非常好。

对于OBJ-C

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(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];
        }
         return cell;
    }

XCode 7.1 iOS 7,8,9:

把这两行放到你的TabelViewCell中:

    self.layoutMargins = UIEdgeInsetsZero;
    self.preservesSuperviewLayoutMargins = false;

这对我很有用

经过大量调查……

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

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