我有一个应用程序,其中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;
    }

其他回答

只需重写cellForRowAtIndexPath并设置cell。preservessuperviewlayoutmargin = false" and "cell。separatorInset = UIEdgeInsets。零”和“单元格”。layoutmargin = uiedgeinsets . 0 "

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell: LayoutTableViewCell! = tableView.dequeueReusableCell(withIdentifier: "LayoutCell") as? LayoutTableViewCell



    let currentLayout = OrderLayouts()[indexPath.row]

    cell.NameLabel?.text = currentLayout.name
    cell.DescrLabel?.text = currentLayout.descr

    if(GlobalVariables.debug){

        cell.DescrLabel?.text = "\(currentLayout.id) \n \(currentLayout.descr)"

    }

    cell.preservesSuperviewLayoutMargins = false
    cell.separatorInset = UIEdgeInsets.zero
    cell.layoutMargins = UIEdgeInsets.zero


    return cell

}

我的解决方案是添加一个UIView作为单元格子视图的容器。然后设置这个UIView约束(顶部,底部,尾部,前导)为0点。所有不必要的质量都消失了。显示逻辑的图像

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

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

在Swift中你可以使用这个

    cell.selectionStyle = UITableViewCellSelectionStyle.None
    cell.preservesSuperviewLayoutMargins = false
    cell.separatorInset = UIEdgeInsetsZero
    cell.layoutMargins = 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);