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

如何删除UITableViewCell分隔符边距?


当前回答

iOS在单元格和表视图上引入了layoutmargin属性。

这个属性在iOS 7.0中不可用,所以你需要确保在分配它之前检查!

然而,苹果已经添加了一个名为preservessuperviewlayoutmargin的属性到你的单元格,这将阻止它继承你的表视图的边距设置。这样,单元格可以独立于表视图配置它们自己的边距。把它看作是一个重写。

这个属性叫做preservessuperviewlayoutmargin,将它设置为NO可以让你用你自己单元格的layoutMargin设置来覆盖你的表格视图的layoutMargin设置。它既节省时间(你不必修改表视图的设置),也更简洁。详情请参考Mike Abdullah的回答。

注意:这是正确的,较少混乱的实现,如Mike Abdullah的回答所示;设置你的单元格的preservessuperviewlayoutmargin =NO将确保你的表格视图不会覆盖单元格设置。

第一步-设置单元格边距:

/*
    Tells the delegate that the table view is about to draw a cell for a particular row.
*/
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
    forRowAtIndexPath indexPath: NSIndexPath)
{
    // Remove separator inset
    if cell.respondsToSelector("setSeparatorInset:") {
        cell.separatorInset = UIEdgeInsetsZero
    }

    // Prevent the cell from inheriting the Table View's margin settings
    if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
        cell.preservesSuperviewLayoutMargins = false
    }

    // Explictly set your cell's layout margins
    if cell.respondsToSelector("setLayoutMargins:") {
        cell.layoutMargins = UIEdgeInsetsZero
    }
}

将单元格上的preservessuperviewlayoutmargin属性设置为NO可以防止表视图覆盖单元格边缘。在某些情况下,它似乎不能正常工作。

第二步——只有在所有失败的情况下,你才可以强制使用你的表视图边距:

/*
    Called to notify the view controller that its view has just laid out its subviews.
*/
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    // Force your tableview margins (this may be a bad idea)
    if self.tableView.respondsToSelector("setSeparatorInset:") {
        self.tableView.separatorInset = UIEdgeInsetsZero
    }

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

...好了!这应该可以在iOS 8和iOS 7上运行。

注意:使用iOS 8.1和7.1进行测试,在我的情况下,我只需要使用本解释的第一步。

第二步只需要在渲染的单元格下面有未填充的单元格。如果表的行数大于表模型中的行数。不执行第二步将导致分隔符偏移量不同。

其他回答

在Swift中你可以使用这个

    cell.selectionStyle = UITableViewCellSelectionStyle.None
    cell.preservesSuperviewLayoutMargins = false
    cell.separatorInset = UIEdgeInsetsZero
    cell.layoutMargins = UIEdgeInsetsZero

这里有一个简单的方法全局删除嵌入。

在UITableViewCell +移到swift:

import UIKit

extension UITableViewCell {

  override public var layoutMargins: UIEdgeInsets {
    get { return UIEdgeInsetsZero }
    set { }
  }

}

在AppDelegate应用中:didFinishLaunchingWithOptions

  UITableViewCell.appearance().separatorInset = UIEdgeInsetsZero

你可能会想a)也只是覆盖separatorInset在扩展,或b)设置外观代理的layoutmargin,而不是。这两种方法都行不通。即使separatorInset被指示为属性,尝试将其作为属性(或方法)重写也会产生编译器错误。并且为UITableViewCell的layoutmargin设置外观代理(或者,也为UITableView的layoutmargin和separatorInset设置外观代理)没有效果。

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

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

只需重写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

}

以上的解决方案我都没有什么运气。我正在为我的表格单元格使用NIB文件。我通过添加一个高度为1的标签来“修复”这个问题。我把标签的背景改为黑色,把标签钉在笔尖的底部,然后把剩下的内容钉在添加的标签上。现在我在单元格的底部有了一条黑色边框。

对我来说,这感觉更像是一种黑客,但它确实有效。

我唯一的选择就是完全消除边界。我还没决定要不要这么做。