我有一个应用程序,其中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进行测试,在我的情况下,我只需要使用本解释的第一步。

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

其他回答

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

- (UIEdgeInsets)layoutMargins
{
    return UIEdgeInsetsZero;
}

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

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中,这有点烦人,因为layoutmargin是一个属性,所以你必须重写getter和setter。

override var layoutMargins: UIEdgeInsets {
  get { return UIEdgeInsetsZero }
  set(newVal) {}
}

这将有效地使layoutmargin为只读,这在我的情况下是好的。

在3楼看到答案后,我试图弄清楚设置TableView和TableViewCell之间的分隔符是什么关系,并做了一些测试。以下是我的结论:

we can consider that setting the cell's separator to zero has to move the separator in two steps: first step is to set cell's separatorinset to zero. second step is to set cell's marginlayout to zero. set the TableView's separatorinset and marginlayout can affect the Cell's separatorinset. However, from the test, I find that the TableView's separatorinset seem to be useless, TableView's marginlayout can actually affect cell's marginlayout. set Cell's PreservesSuperviewLayoutMargins = false, can cut off TableView's marginlayout effect on Cells. one of the solutions: func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell = UITableViewCell() cell.preservesSuperviewLayoutMargins = false cell.separatorInset = UIEdgeInsetsZero cell.layoutMargins = UIEdgeInsetsZero return cell }

这是在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)
        }
    }