如何在UITableView中设置分隔符的整个宽度

Tim*_*giz 142 iphone objective-c uitableview ios

所以我有一个UITableView,其中分隔符没有全宽.它在左侧前10像素结束.我在viewDidLoad中玩这个代码

self.tableView.layoutMargins = UIEdgeInsetsZero;
Run Code Online (Sandbox Code Playgroud)

也可以在故事板中选择自定义或默认选择器.现在,所有填充的单元格都没有全宽度选择器,但是空单元格具有全宽度.

我怎么能解决这个问题呢?

谢谢你的帮助

MB_*_*per 214

这适用于使用Xcode 6.4和Swift 1.2的iOS 8.4 - 9.0设备:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = UITableViewCell()


    cell.preservesSuperviewLayoutMargins = false
    cell.separatorInset = UIEdgeInsetsZero
    cell.layoutMargins = UIEdgeInsetsZero

    return cell
}
Run Code Online (Sandbox Code Playgroud)

Swift 3.0更新

cell.preservesSuperviewLayoutMargins = false
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero
Run Code Online (Sandbox Code Playgroud)


Tim*_*giz 102

好的,我找到了答案.不知道为什么我以前没有遇到这个帖子,但当然在你发布一个问题之后,突然之后,正确的帖子就会出现在你面前.我从这篇文章得到了答案:iOS 8 UITableView分隔符插入0无法正常工作

只需将此代码添加到您的 UITableViewController

-(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];
    }
}

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

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

  • 对我来说,方法`viewDidLayoutSubviews`不是必需的 (3认同)
  • 它适用于我只添加tableView委托方法 (2认同)

Han*_*ney 95

在你的 UITableViewCell

转到Interface Builder中的Attributes Inspector,只需将"15"更改为0.对要更改的所有单元格执行此操作.

instets

您可能需要添加[cell setLayoutMargins:UIEdgeInsetsZero];到您的tableViewCell


Wil*_*son 27

对于Swift 3:

override func viewDidLoad() {
  super.viewDidLoad()

  tableView.separatorInset = .zero
  tableView.layoutMargins = .zero
}
Run Code Online (Sandbox Code Playgroud)


H6.*_*H6. 19

我继承UITableViewController并需要额外的两个insets设置willDisplayCell也设置preservesSuperviewLayoutMargins为false.在Swift中它看起来像这样:

override func tableView(_tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

    if cell.respondsToSelector("setSeparatorInset:") {
        cell.separatorInset = UIEdgeInsetsZero
    }
    if cell.respondsToSelector("setLayoutMargins:") {
        cell.layoutMargins = UIEdgeInsetsZero
    }
    if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
        cell.preservesSuperviewLayoutMargins = false
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果单元格未构成完整高度,则不会调整控制器显示的"占位符"单元格上的分隔符以填充单元格下方的视图. (2认同)

Seb*_*ara 18

  1. 选择你 UITableViewCell
  2. 转到Atributes Inspector
  3. 转到分隔符并将其更改为"自定义插入"
  4. 设置left和/或right字段.(默认情况下left: 15,right: 0)

看看它对我有用(使用left: 100):

在此输入图像描述

结果:

在此输入图像描述


Ram*_*min 11

Inset默认情况下,分隔符从左起是15。将Separator Inset选项从更改autocustom,并将插图设置为0

在此处输入图片说明


Mic*_*ose 9

对于有iPad问题的人 - 这将使您处于与iPhone相同的状态.然后,您可以根据需要调整separatorInset.

tableView.cellLayoutMarginsFollowReadableWidth = false
Run Code Online (Sandbox Code Playgroud)


oya*_*lhi 8

经过iOS 9.3和Swift 2.2测试.确保willDisplayCell在显示单元格之前放置调用的代码,而不是仅在cellForRowAtIndexPath创建单元格的位置.

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.separatorInset = UIEdgeInsetsZero
    cell.layoutMargins = UIEdgeInsetsZero
}
Run Code Online (Sandbox Code Playgroud)

添加override到UITableViewController的函数,如下所示:UITableViewController


Chr*_*sen 8

适用于iOS 9+中的swift

如果使用自定义UITableViewCell:

override var layoutMargins: UIEdgeInsets {
    get { return UIEdgeInsetsZero }
    set(newVal) {}
}
Run Code Online (Sandbox Code Playgroud)

然后为你UITableViewviewDidLoad:

self.tableView?.separatorInset = UIEdgeInsetsZero;
self.tableView?.layoutMargins = UIEdgeInsetsZero;
Run Code Online (Sandbox Code Playgroud)


Aks*_*Aks 6

cellForRowAtIndexPath方法中使用它来配置单元格的分隔符规格,
它适用于iOS9.0 +

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


Dev*_*ian 6

快速 5,Xcode 11 更新

把它放在 viewDidLoad() 里面

yourTableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
Run Code Online (Sandbox Code Playgroud)

对于边到边分隔符,只需将 left 和 right 的值设置为 0。

顺便将“yourTableView”更改为您的tableView Outlet 名称。


aur*_*ing 5

以上都不适用于Swift 2.2和Xcode 7.3.1

原来是所有人中最简单的解决方案.无需代码.只需TableViewCellUITableView检查器中更改布局边距值: