简体   繁体   English

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

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

I have a UITableView where the separators don't have the full width.我有一个UITableView ,其中分隔符没有全宽。 It ends like 10 pixels before the left side.它在左侧前 10 个像素处结束。 I was playing around with this code in the viewDidLoad() .我在viewDidLoad()玩弄这段代码。

self.tableView.layoutMargins = UIEdgeInsetsZero;

Also in the storyboard when you can select custom or default selectors.同样在故事板中,您可以选择自定义或默认选择器。 Now all the cells that are populated don't have the full-width selectors but the cells that are empty have full width.现在填充的所有单元格都没有全宽选择器,但空的单元格具有全宽。

How can I fix this?我怎样才能解决这个问题?

This worked for me on iOS 8.4 - 9.0 devices using Xcode 6.4 and Swift 1.2:这在使用 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
}

Swift 5 Update:斯威夫特 5 更新:

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

I got the answer from this post: iOS 8 UITableView separator inset 0 not working我从这篇文章中得到了答案: iOS 8 UITableView separator inset 0 not working

Just add this code on your UITableViewController只需在您的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];
    }
}

In your UITableViewCell在你的UITableViewCell

Go to Attributes Inspector in your Interface Builder and simply change "15" to 0. Do this for all the cells you wish to change.转到界面生成器中的 Attributes Inspector 并将“15”更改为 0。对您希望更改的所有单元格执行此操作。

插入

You may need to add [cell setLayoutMargins:UIEdgeInsetsZero];您可能需要添加[cell setLayoutMargins:UIEdgeInsetsZero]; to your tableViewCell到您的tableViewCell

for Swift 3 :对于 Swift 3 :

override func viewDidLoad() {
  super.viewDidLoad()

  tableView.separatorInset = .zero
  tableView.layoutMargins = .zero
}
  1. Select your UITableViewCell选择你的UITableViewCell
  2. Go to Atributes Inspector转到属性检查器
  3. Go to Separator and change it to "custom Insets"转到分隔符并将其更改为“自定义插入”
  4. Set left and / or right fields.设置left和/或right字段。 (By default left: 15 , right: 0 ) (默认left: 15right: 0

Look how it works (using left: 100 ):看看它是如何工作的(使用left: 100 ):

在此处输入图片说明

Result:结果:

在此处输入图片说明

I inherit from UITableViewController and needed additional to the two insets settings in willDisplayCell also to set preservesSuperviewLayoutMargins to false.我从UITableViewController继承,并且除了willDisplayCell的两个 insets 设置willDisplayCell还需要将preservesSuperviewLayoutMargins设置为 false。 In Swift it looks like this:在 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
    }
}

The Separator Inset is by default 15 from left.分隔符Inset默认为从左侧开始 15。 Change Separator Inset option from auto to custom and set the inset to 0 .将 Separator Inset选项从auto更改为custom并将 inset 设置为0

在此处输入图片说明

For Swift in iOS 9+适用于 iOS 9+ 中的 Swift

If using a custom UITableViewCell :如果使用自定义UITableViewCell

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

then for your UITableView in viewDidLoad :然后对于viewDidLoadUITableView

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

Use it in cellForRowAtIndexPath method, to configure cell's separator specs ,cellForRowAtIndexPath方法中使用它,配置单元格的分隔符规格,
it works perfect for iOS9.0+它适用于 iOS9.0+

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

For people having issues with the iPad — this will get you in a state that is the same as the iPhone.对于 iPad 有问题的人——这会让你处于与 iPhone 相同的状态。 You can then adjust the separatorInset as needed.然后,您可以根据需要调整separatorInset

tableView.cellLayoutMarginsFollowReadableWidth = false

Tested for iOS 9.3 & Swift 2.2.已针对 iOS 9.3 和 Swift 2.2 进行测试。 Make sure to put the code in willDisplayCell which is called just before displaying the cell and not in cellForRowAtIndexPath where you create the cell only.确保将代码放在willDisplayCell ,该代码在显示单元格之前被调用,而不是放在仅创建单元格的cellForRowAtIndexPath中。

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

Add override to the function for UITableViewController , like so:UITableViewController的函数添加override ,如下所示:

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

swift 5, Xcode 11 Update快速 5,Xcode 11 更新

place this inside viewDidLoad()把它放在 viewDidLoad() 里面

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

for edge to edge separator just set the value of left and right to 0.对于边到边分隔符,只需将 left 和 right 的值设置为 0。

by the way change the "yourTableView" to your tableView Outlet name.顺便将“yourTableView”更改为您的tableView Outlet 名称。

None of the above worked for me in Swift 2.2 and Xcode 7.3.1以上在 Swift 2.2 和 Xcode 7.3.1 中都不适合我

Turned out to be the simplest solution of all.结果证明这是最简单的解决方案。 No code needed.不需要代码。 Just change TableViewCell Layout Margins values in your UITableView inspector:只需在UITableView检查器中更改TableViewCell Layout Margins 值:

For Swift 3 :对于 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
    }
}

In viewDidLoad (tested iOS11 - swift 4.1)viewDidLoad (测试 iOS11 - swift 4.1)

try尝试

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

None of these solutions work on the iPad, but I have come up with a solution that covers both devices:这些解决方案都不适用于 iPad,但我提出了一个涵盖两种设备的解决方案:

With reusable cells:使用可重复使用的细胞:

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

With non reusable cells:对于不可重复使用的单元格:

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

Just to expand on this approach:只是为了扩展这种方法:

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

Both properties can be used by UITableView & UITableViewCell . UITableViewUITableViewCell可以使用这两个属性。 The latter is, in fact, a property of UIView , which is a parent class of both UITableView & UITableViewCell .后者实际上是UIView一个属性,它是UITableViewUITableViewCell的父类。

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

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