繁体   English   中英

如何在 UITableView 中设置分隔符的全宽

[英]How to set the full width of separator in UITableView

我有一个UITableView ,其中分隔符没有全宽。 它在左侧前 10 个像素处结束。 我在viewDidLoad()玩弄这段代码。

self.tableView.layoutMargins = UIEdgeInsetsZero;

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

我怎样才能解决这个问题?

这在使用 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
}

斯威夫特 5 更新:

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

我从这篇文章中得到了答案: iOS 8 UITableView separator inset 0 not working

只需在您的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];
    }
}

在你的UITableViewCell

转到界面生成器中的 Attributes Inspector 并将“15”更改为 0。对您希望更改的所有单元格执行此操作。

插入

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

对于 Swift 3 :

override func viewDidLoad() {
  super.viewDidLoad()

  tableView.separatorInset = .zero
  tableView.layoutMargins = .zero
}
  1. 选择你的UITableViewCell
  2. 转到属性检查器
  3. 转到分隔符并将其更改为“自定义插入”
  4. 设置left和/或right字段。 (默认left: 15right: 0

看看它是如何工作的(使用left: 100 ):

在此处输入图片说明

结果:

在此处输入图片说明

我从UITableViewController继承,并且除了willDisplayCell的两个 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
    }
}

分隔符Inset默认为从左侧开始 15。 将 Separator Inset选项从auto更改为custom并将 inset 设置为0

在此处输入图片说明

适用于 iOS 9+ 中的 Swift

如果使用自定义UITableViewCell

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

然后对于viewDidLoadUITableView

self.tableView?.separatorInset = UIEdgeInsetsZero;
self.tableView?.layoutMargins = UIEdgeInsetsZero;

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

 cell.separatorInset = UIEdgeInsetsZero;
 cell.layoutMargins = UIEdgeInsetsZero;
 cell.preservesSuperviewLayoutMargins = NO;

对于 iPad 有问题的人——这会让你处于与 iPhone 相同的状态。 然后,您可以根据需要调整separatorInset

tableView.cellLayoutMarginsFollowReadableWidth = false

已针对 iOS 9.3 和 Swift 2.2 进行测试。 确保将代码放在willDisplayCell ,该代码在显示单元格之前被调用,而不是放在仅创建单元格的cellForRowAtIndexPath中。

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

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

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

快速 5,Xcode 11 更新

把它放在 viewDidLoad() 里面

yourTableView.separatorInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

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

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

以上在 Swift 2.2 和 Xcode 7.3.1 中都不适合我

结果证明这是最简单的解决方案。 不需要代码。 只需在UITableView检查器中更改TableViewCell Layout Margins 值:

对于 Swift 3 :

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
        cell.separatorInset = UIEdgeInsets.zero
    }
    if cell.responds(to: #selector(setter: UITableViewCell.layoutMargins)) {
        cell.layoutMargins = UIEdgeInsets.zero
    }
    if cell.responds(to: #selector(setter: UITableViewCell.preservesSuperviewLayoutMargins)) {
        cell.preservesSuperviewLayoutMargins = false
    }
}

viewDidLoad (测试 iOS11 - swift 4.1)

尝试

tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)

这些解决方案都不适用于 iPad,但我提出了一个涵盖两种设备的解决方案:

使用可重复使用的细胞:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    ...[other code]...
    [cell setLayoutMargins:UIEdgeInsetsZero];
    [cell setSeparatorInset:UIEdgeInsetsZero];
    return cell;
}

对于不可重复使用的单元格:

- (void)removeSeparatorInset:(UITableView*)tableView{
    NSArray *cells = [tableView visibleCells];
    for (UITableViewCell *cell in cells){
        [cell setLayoutMargins:UIEdgeInsetsZero];
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
}

-(void) viewDidLayoutSubviews{
   [super viewDidLayoutSubviews];
   [self removeSeparatorInset:self.tableView];
}

只是为了扩展这种方法:

@property(nonatomic) UIEdgeInsets separatorInset;
@property(nonatomic) UIEdgeInsets layoutMargins;

UITableViewUITableViewCell可以使用这两个属性。 后者实际上是UIView一个属性,它是UITableViewUITableViewCell的父类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM