ios 如何删除 UITableView 中最后一个单元格的最后一个边框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12168002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:55:20  来源:igfitidea点击:

How to remove the last border of the last cell in UITableView?

iphoneobjective-ciosxcodeuitableview

提问by Tonny Xu

In my app, I use a UITableViewMy problem is that I want to remove the last border of the last cell in UITableView.

在我的应用程序中,我使用了一个UITableView我的问题是我想删除UITableView.

Please check the following image:

请检查以下图像:

enter image description here

在此处输入图片说明

采纳答案by onegray

Updated on 9/14/15. My original answer become obsolete, but it is still a universal solution for all iOS versions:

2015 年 9 月 14 日更新。我原来的答案已经过时了,但它仍然是所有 iOS 版本的通用解决方案:

You can hide tableView's standard separator line, and add your custom line at the top of each cell. The easiest way to add custom separator is to add simple UIViewof 1px height:

您可以隐藏tableView的标准分隔线,并在每个单元格的顶部添加自定义线。添加自定义分隔符的最简单方法是添加简单UIView的 1px 高度:

UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, 1)];
separatorLineView.backgroundColor = [UIColor grayColor];
[cell.contentView addSubview:separatorLineView];

To date, I subscribe to another wayfor hiding extra separators below cells (works for iOS 6.1+):

迄今为止,我订阅了另一种隐藏单元格下方额外分隔符的方法(适用于 iOS 6.1+):

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

回答by Tonny Xu

The best solution is add a footer view. The following code hide the last cell's line perfectly:

最好的解决方案是添加页脚视图。以下代码完美地隐藏了最后一个单元格的行:

Objective-C

目标-C

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)];

Swift 4.0

斯威夫特 4.0

tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))

回答by davidisdk

In iOS 7 there is an easier solution. Supposing cell is your last cell:

在 iOS 7 中有一个更简单的解决方案。假设单元格是您的最后一个单元格:

cell.separatorInset = UIEdgeInsetsMake(0, cell.bounds.size.width, 0, 0);

回答by Jamie Forrest

This works for me in iOS 7 and 8:

这在 iOS 7 和 8 中对我有用:

cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, CGRectGetWidth(tableView.bounds));

NOTE: be careful about using tableView.boundsas it sometimes reports the wrong value depending on when you call it. See Reporting incorrect bounds in landscape Mode

注意:使用时要小心,tableView.bounds因为它有时会根据您调用它的时间报告错误的值。请参阅在横向模式下报告错误的边界

回答by Jonny

Add this line of code in viewDidLoad().

viewDidLoad().

tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0.001))

This make sure the last separator will be replaced and replaced (invisible) footer view will not occupy any extra height. Since the footer view's width will be managed by UITableView, so you can set it to 0.

这确保最后一个分隔符将被替换,并且被替换的(不可见的)页脚视图不会占用任何额外的高度。由于页脚视图的宽度将由 管理UITableView,因此您可以将其设置为 0。

回答by fl034

Extension based solution for Swift 4, tested on iOS 12

基于扩展的 Swift 4 解决方案,在 iOS 12 上测试

I noticed that setting a empty view of height = 1also removes the separator of the last visible cell however setting height = 0just removes the separator of the empty cells

我注意到设置一个空视图height = 1也会删除最后一个可见单元格height = 0的分隔符,但是设置只会删除空单元格的分隔符

extension UITableView {
    func removeSeparatorsOfEmptyCells() {
        tableFooterView = UIView(frame: .zero)
    }

    func removeSeparatorsOfEmptyCellsAndLastCell() {
        tableFooterView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 0, height: 1)))
    }
}

回答by Marco Mardegan

My shorter version:

我的较短版本:

self.tblView.tableFooterView = [UIView new];

回答by DungeonDev

In Swift simply:

在 Swift 中简单地说:

tableView.tableFooterView = UIView()

回答by rtato

Here is the remedy I currently use. it might not be the best approach, but it works.

这是我目前使用的补救措施。这可能不是最好的方法,但它有效。

Remove SeparatorColor and setSeparatorStyle to make custom separator

删除 SeparatorColor 和 setSeparatorStyle 以制作自定义分隔符

- (void)viewDidLoad
{
    [super viewDidLoad];

    ...

    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableView setSeparatorColor:[UIColor clearColor]];
}

Add custom separator to each cell with tableview background

为每个带有 tableview 背景的单元格添加自定义分隔符

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    ...

    UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.frame.size.height-1, 320, 1)];
    separatorLineView.backgroundColor = self.tableView.backgroundColor;
    [cell.contentView addSubview:separatorLineView];

    return cell;
}

回答by ohana

_tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];