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

Posted

技术标签:

【中文标题】如何删除UITableView中最后一个单元格的最后一个边框?【英文标题】:How to remove the last border of the last cell in UITableView? 【发布时间】:2012-08-23 11:26:58 【问题描述】:

在我的应用程序中,我使用 UITableView 我的问题是我想删除UITableView中最后一个单元格的最后一个边框。

请检查以下图片:

【问题讨论】:

更改 separatorInset 如此答案 ***.com/a/8561820/1418457 【参考方案1】:

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

Objective-C

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

Swift 4.0

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

【讨论】:

只有一个部分才有效 @GoodSp33d 我有多个部分,它通过这种方式工作if(indexPath.row == [self.tableView numberOfRowsInSection:indexPath.section] - 1) self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1)]; CellForRowAtIndexPath 数据源中 我的坏评论早早的。仅添加该行,我猜将默认高度设置为 44 像素,因此我想连同那行代码一起将1.0f 作为-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section 委托中的页脚高度传递。现在它就像一个魅力。赞成 令人难以置信的是,如今仍然需要这种 hack...【参考方案2】:

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

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

【讨论】:

这隐藏了我的单元格的全部内容。标签变得不可见。 不起作用,它只是将标签向右移动。 当我将它应用到底部单元格时,在 iOS 7 上非常适合我使用具有固定数量单元格的非滚动表。 不能与“标准”单元格一起使用,因为插图决定了标签的位置。可与客户单元一起使用。【参考方案3】:

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

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

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

迄今为止,我订阅了 another way 以在单元格下方隐藏额外的分隔符(适用于 iOS 6.1+):

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

【讨论】:

CGRectZero 方法对我不起作用。然而,下面将框架设置为 CGRectMake(0, 0, self.tableView.frame.size.width, 1) 的另一个答案确实有效。 天才回答!拯救了我的一天。 厄瓜多尔的评论是对的。答案在 iOS 12 上不起作用,不应标记为正确,因为它只是从空单元格中删除分隔符。【参考方案4】:

这适用于 iOS 7 和 8:

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

注意:使用tableView.bounds 时要小心,因为它有时会根据您调用它的时间报告错误的值。见Reporting incorrect bounds in landscape Mode

【讨论】:

在 iOS8 中也能完美运行。谢谢 我用它来避免 5.5 英寸屏幕上的奇怪行为:cell!.separatorInset = UIEdgeInsetsMake(0, 0, 0, CGRectGetWidth(self.tableView.bounds)-self.view.layoutMargins.left) 当方向先是纵向,然后是横向时,会出现线条【参考方案5】:

viewDidLoad()中添加这行代码。

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

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

【讨论】:

唯一对我有用的解决方案,.zero 在表格底部留下了一个分隔符。 @Ido 你好,只是为了让你知道我回滚了你的编辑版本,因为我不认为你添加的 CGRect 扩展是一个很好的做法。如果你不喜欢示例代码中那些繁琐的0,你可以这样写:var frame = CGRect.zero \n frame.size.height = 0.001 请问为什么这不是一个好习惯?它让您可以使用与 .zero 相同的方式,但使用实际的零 (0.001) 来满足您的需要。 因为它不够灵活(如果有一天你想要一个宽度为 0.001 的框架怎么办?),而属性名称 CGRect.myZero 并不能很好地说明它可以做什么。跨度> 你可以称它为almostZero,如果那是它不灵活的原因。您可以在static let ... 上方添加一些评论来描述它(使用 3 / 这样您就可以通过 xCode 的自动完成界面看到它)。【参考方案6】:

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

我注意到设置 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)))
    

【讨论】:

谢谢。事实证明,任何正的、非零的高度都可以。例如高 0.1 分。【参考方案7】:

我的短版:

self.tblView.tableFooterView = [UIView new];

【讨论】:

【参考方案8】:

在 Swift 中:

tableView.tableFooterView = UIView()

【讨论】:

你在使用 Swift 3 吗? 这对我有用。请注意,我使用的是 UITableViewController 的子类 iOS 13.4.1 不工作但 ``` tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: CGFloat.leastNonzeroMagnitude, height: .leastNonzeroMagnitude)) `` `【参考方案9】:

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

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

- (void)viewDidLoad

    [super viewDidLoad];

    ...

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

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

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

【讨论】:

这不适用于具有自定义高度的单元格 - 在您获取单元格高度时,单元格尚未完全布局。此外,您不应该使用像 320 这样的硬编码值 - 这在 iPhone 6 上看起来如何?【参考方案10】:

适用于 iOS 7 及更高版本

    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
    
        BOOL bIsLastRow = NO;

        //Add logic to check last row for your data source
        NSDictionary *dict = [_arrDataSource objectAtIndex:indexPath.section];
        if([_arrDataSource lastObject] == dict)
        
           bIsLastRow = YES;
        

        //Set last row separate inset left value large, so it will go outside of view
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) 
        
            [cell setSeparatorInset:UIEdgeInsetsMake(0, bIsLastRow ? 1000 :0, 0, 0)];
        
    

【讨论】:

【参考方案11】:

斯威夫特 4.2

要在最后一个单元格中获得从左到右的分隔符,请将其添加到您的 UITableViewDataSource's tableView(_:cellForRowAt:) 实现中:

if tableView.numberOfRows(inSection: indexPath.section) - 1 == indexPath.row 
        cell.separatorInset = .zero

如果您不想为某个特定单元格设置分隔符,请考虑绘制自己的分隔符并设置tableView.separatorStyle = .none

【讨论】:

【参考方案12】:
_tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

【讨论】:

您好,欢迎来到 ***,感谢您的回答。虽然这段代码可能会回答这个问题,但您是否可以考虑添加一些解释来说明您解决了什么问题,以及您是如何解决的?这将有助于未来的读者更好地理解您的答案并从中学习。【参考方案13】:

下面的代码帮助我从 UITableView 的最后一行删除分隔符。

斯威夫特 3.0

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) 
    if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) 
        cell.separatorInset.right = cell.bounds.size.width
    

【讨论】:

【参考方案14】:

Xcode 12、Swift 5

要删除最后一个单元格后的分隔符,请选择分组的 UITableView 样式 You can do that in Interface Builder 或在 viewDidLoad 中设置

tableView.style = UITableView.Style.grouped

删除第一个单元格之前的空白,在 viewDidLoad 中编写此代码:

tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.width, height: 0.1))

要删除单元格之前的空白空间,并且您要在导航栏下滚动它或在避免空白空间方面遇到其他困难,请尝试将您的表格限制为控制器的超级视图 Here double click the top constraint 然后选择 'SuperView.Top' at the constraint settings

如果您想要全宽分隔符或您自己的宽度,只需选择Separator Inset作为自定义,并配置为您的值,或将其写入viewDidLoad:

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

【讨论】:

【参考方案15】:

cellForRow 委托中找到最后一个单元格索引,并将代码行放在下面:

cell.separatorInset = UIEdgeInsetsMake(
    0.f,
    40.0f,
    0.f,
    self.view.frame.size.width-40.0f
);

【讨论】:

【参考方案16】:

另一种选择是继承UITableView

@synthesize hideLastSeparator = _hideLastSeparator;
@synthesize hideLastSeparatorView = _hideLastSeparatorView;
-(void)setHideLastSeparator:(BOOL)hideLastSeparator 
    if (_hideLastSeparator == hideLastSeparator) 
        return;
    
    _hideLastSeparator = hideLastSeparator;
    if (_hideLastSeparator) 
        _hideLastSeparatorView = [[UIView alloc] initWithFrame:CGRectMake(0, self.contentSize.height, self.bounds.size.width, 0.5f)];
        _hideLastSeparatorView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
        _hideLastSeparatorView.backgroundColor = self.backgroundColor;
        [self addSubview:_hideLastSeparatorView];
        [self hideSeparator];
    
    else 
        [_hideLastSeparatorView removeFromSuperview];
        _hideLastSeparatorView = nil;
    

-(void)setContentSize:(CGSize)contentSize 
    [super setContentSize:contentSize];
    if (_hideLastSeparator) 
        [self hideSeparator];
    

-(void)hideSeparator 
    CGRect frame = _hideLastSeparatorView.frame;
    frame.origin.y = self.contentSize.height - frame.size.height;
    _hideLastSeparatorView.frame = frame;

.h 应该只包含hideLastSeparatorhideLastSeparatorView 的属性声明。 当想要隐藏分隔符时,使用新类并设置myTableView.hideLastSeparator = YES。 它的工作方式是通过在最后一个分隔符上添加一个新视图来阻止它。

在我看来这更容易使用(比使用自定义分隔符,或设置最后一个单元格的最后一个 separatorInset),并且避免了设置 tableFooterView 的方法有时会导致的一些奇怪的动画(例如在行期间插入/删除或其他表格动画)。

【讨论】:

【参考方案17】:

如果您为 UITableViewCell 使用自定义分隔符,最好的解决方案是:

    在您的自定义单元格函数中实现,隐藏separatorView
class YourCell: UITableViewCell 
    // Your implementation goes here...
    // Separator view in cell
    @IBOutlet private weak var separatorView: UIView!

    // This function hides the separator view
    func hideSeparator() 
        separatorView.isHidden = true
    

    tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) 中计算单元格是否是最后一个单元格并隐藏它的分隔符
public func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) 
    guard let cell = cell as? YourCell else 
            return
    
    // Here we're checking if your cell is the last one
    if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 
        // if true -> then hide it
        cell.hideSeparator()
    

【讨论】:

【参考方案18】:

这对我很有用:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) 
    guard let cell = cell as? yourCell else 
            return
    
    // Here we're checking if your cell is the last one
    if indexPath.row == numberOfCells.count - 1 
        cell.separatorInset.left = UIScreen.main.bounds.width
    

我们在这里所做的是将左插图的大小设置为足够大的值,以便分隔线不再可见。因此,当我们增加 inset 的 size 值时,它会越来越靠近屏幕右侧,因为左 inset 朝着正确的方向。

【讨论】:

【参考方案19】:

最合适和最简单的方法是在cellForRowAt使用它

cell.drawBottonLine = (indexPath.row != sections[0].rows.count - 1)

【讨论】:

【参考方案20】:

扩展 Tonny Xu 的回答,我有多个部分,这似乎适用于删除最后一个单元格分隔符

if(indexPath.row == [self.tableView numberOfRowsInSection:indexPath.section] - 1)

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

cellForRowAtIndexPath 数据源中

【讨论】:

这取决于一些隐藏的实现细节——你不应该这样做; tableFooterView 存在于整个表格中,而不仅仅是最后一个单元格。总的来说,hacky 解决方案,可能并不总是有效。

以上是关于如何删除UITableView中最后一个单元格的最后一个边框?的主要内容,如果未能解决你的问题,请参考以下文章

向下滚动时,UITableview 看不到最后一个单元格

UITableView 在删除一行或多行后留下空白单元格的模式

在 UITableView 中重新排序单元格会取消 Swift 中单元格的功能?

UITableView 重复单元格如何防止呢?迅速

如何在 Swift 中动态更改包含 UICollectionView 单元格的 UITableView 单元格的高度?

如何在 UITableView 部分之间添加空间?