分隔线前的空格分为我的TableView

Saf*_*ari 45 iphone interface-builder uitableview ios

我有一个关于UITableView的问题...我有一个UITableViewController,我创建了一个自定义单元格.当我可视化tableView时,我会在分隔线之前看到一个小空格,您可以在此屏幕截图中看到:

在此输入图像描述

为什么?这是默认的可视化?我可以更改一些东西来移除这个白色的左边填充物吗?

Ash*_*hok 89

iOS 7中默认提供前导空格,即使是自定义单元格也是如此.

检查separatorInsetUITableviewCell的此属性以删除/添加单元格行分隔符两端的白色间距.

//删除空格

cell.separatorInset = UIEdgeInsetsZero;
Run Code Online (Sandbox Code Playgroud)

或者,在UITableView级别,您可以使用此属性 -

if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {  // Safety check for below iOS 7 
    [tableView setSeparatorInset:UIEdgeInsetsZero];
}
Run Code Online (Sandbox Code Playgroud)

更新 - 以下代码适用于iOS 7和iOS 8:

-(void)viewDidLayoutSubviews
{
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }

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

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
Run Code Online (Sandbox Code Playgroud)


mic*_*ila 34

或者,您也可以在界面构建器(IB)中编辑它:

  1. 去IB吧.
  2. 选择表格视图.
  3. 打开右侧的"属性检查器".
  4. 将"分隔符插入"从"默认"更改为"自定义".
  5. 将"Left"属性从15更改为0.

这与@Ashok的答案具有相同的效果,但不需要编写任何代码.

iOS 7和8上的更新工作

iOS 9上的更新工作

  • 这不起作用.具体来说,它在左边缘留下3或4 pts/pxs的空间. (5认同)
  • 这在某种程度上对我不起作用,似乎是一个bug (3认同)
  • 这适用于Xcode 7,iOS 9. Upvoted.这比通过代码执行它更清晰. (2认同)

Ano*_* VM 16

使用下面的代码删除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];
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 有趣的是,这对我有用,而其他建议没有. (2认同)

Dav*_*d H 10

没有空白区域!我输入了一个错误,Apple只是把它关闭为"不是一个bug",但告诉我为什么它不是一个bug.我写了一个简单的项目,为应用程序中的每个可能的视图设置颜色.我看到的是那些像素的颜色实际上是细胞本身的背景颜色(不是内容视图!),正如Apple告诉我的那样.

cell.contentView.backgroundColor为绿色,cell.background颜色为红色(与Pixie一起使用):

在此输入图像描述

最后,如果没有分隔符,cell.contentView将完全填充单元格.使用分隔符时,底部有一个或两个像素的间隙.插入时,分隔符填充了大部分间隙,但是细胞中有一些像素显示出来.

谜团已揭开!

编辑:似乎根据您配置故事板的方式,contentView.backgroundColor"丢失"或设置为白色.如果你在提供细胞时过度骑行,你可以得到你想要的行为:

    let cell = tableView.dequeueReusableCellWithIdentifier("FOO", forIndexPath: indexPath) as UITableViewCell

    cell.backgroundColor = UIColor.redColor()
    cell.contentView.backgroundColor = UIColor.greenColor()
Run Code Online (Sandbox Code Playgroud)


Sha*_*ram 9

迅速

cell.separatorInset = UIEdgeInsetsMake(0, 0, cell.frame.size.width, 0)
if (cell.respondsToSelector("preservesSuperviewLayoutMargins")){
        cell.layoutMargins = UIEdgeInsetsZero
        cell.preservesSuperviewLayoutMargins = false
   }
Run Code Online (Sandbox Code Playgroud)

这个对我有用

谢谢


lg3*_*365 7

这对我有用:

cell?.layoutMargins = UIEdgeInsetsZero;
cell?.preservesSuperviewLayoutMargins = false
Run Code Online (Sandbox Code Playgroud)


eri*_*cgu 6

对于那些想要在Swift中保留UIEdgeInsetSettings并且不使用Storyboard的人:

之前:(默认行为)

之前,默认设置

添加以下代码:

tableView.separatorInset.right = tableView.separatorInset.left
Run Code Online (Sandbox Code Playgroud)

之后,实施