删除表中单元格的最后一个分隔符

Mig*_*Dey 15 uitableview swift ios8

我在我的故事板中设置了静态TableView,并且我试图在最后一个单元格中删除分隔符以用于美学目的.我查看了本网站和其他网站上的各种答案,他们都指出了这种类型的IOS8代码

 self.tableView.tableFooterView = UIView()
Run Code Online (Sandbox Code Playgroud)

但是,当我运行应用程序时,分隔符仍然存在.不太确定我做错了什么.

九月

Iya*_*Iya 36

从最后一个单元格中删除分隔符的一种聪明方法是添加高度为1的普通视图.

tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 1)];
Run Code Online (Sandbox Code Playgroud)


Geo*_*kas 32

Swift 3版本的最高投票答案:

tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))
Run Code Online (Sandbox Code Playgroud)

即使单元格数量是动态的,它也能工作.

  • 它不再适用于 iOS 15.0,我现在正在寻找替代解决方案。 (10认同)
  • 我仍然可以看到最后一行的行 (2认同)

Jon*_*nny 5

我正在使用下面显示的适用于 iOS 15+ 的代码,因为其他解决方案停止工作。

该解决方案需要子类化UITableViewCell,并手动检查单元格创建时间的顶部/底部单元格,因此它不是那么理想。我很想知道更好的解决方案。

class TableViewCell : UITableViewCell {
    
    var hidesTopSeparator = false
    var hidesBottomSeparator = false
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let topSeparator = subviews.first { $0.frame.minY == 0 && $0.frame.height <= 1 }
        let bottomSeparator = subviews.first { $0.frame.minY >= bounds.maxY - 1 && $0.frame.height <= 1 }
        
        topSeparator?.isHidden = hidesTopSeparator
        bottomSeparator?.isHidden = hidesBottomSeparator
    }
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let reuseID = "cell"
    let cell = tableView.dequeueReusableCell(withIdentifier: reuseID) as? TableViewCell ?? .init(style: .default, reuseIdentifier: reuseID)
    
    let numberOfRows = self.tableView(tableView, numberOfRowsInSection: indexPath.section)
    cell.hidesTopSeparator = indexPath.row == 0
    cell.hidesBottomSeparator = indexPath.row == numberOfRows - 1
    
    return cell
}
Run Code Online (Sandbox Code Playgroud)


Mat*_*att 2

此解决方案将删除分组表的每个部分中的最后一个分隔符,而不需要对表或单元格进行子类化。

适用于 iOS 8。(可能适用于 iOS 6+)。

将以下内容添加到您的委托中:

#pragma mark - UITableViewDelegate

- (CGFloat) tableView:(UITableView*)table
        heightForFooterInSection:(NSInteger)section {
     // Hide the separator when the table is first displayed
     // of any sections that are visible on the screen.
     [self hideBottomSeparator:table];

     return 0.01f; // Or whatever you already return.
}


- (void) scrollViewDidScroll:(UITableView*)table {
    // Hide separators that are visible after table scrolls.
    // Check that event did not come from other scrollers
    // in the table.
    if ([table isKindOfClass:UITableView.class]) {
         [self hideBottomSeparator:table];
    }
}

- (void) hideBottomSeparator:(UITableView*)table {
    for (UIView* cell in table.visibleCells) {
         [self removeBottomSeparatorFromCell:cell];
    }
}

- (void) removeBottomSeparatorFromCell:(UITableViewCell*)cell {
    for (UIView* view in cell.subviews) {
        if (view.frame.origin.x == 0 &&
                view.frame.origin.y > cell.frame.size.height - 2 &&
                view.frame.size.width == cell.frame.size.width &&
                view.frame.size.height < 2) {
            [view removeFromSuperview];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它的工作原理是删除UIView可见单元格中全宽和单元格底部的所有细小 s (它使顶部分隔符可见)。

要同时删除顶部分隔符,请注释掉origin.y检查

如果您想从特定部分中删除分隔符,则必须从单元格中获取部分编号。

注意:因为它仅按大小(和位置)删除分隔符,所以对于非默认表格(或单元格)插入值,或者如果 iOS 更改分隔符的外观,它可能会失败。此外,如果 iOS 发生更改,使分隔符不是单元格的子视图,则可能会失败,在这种情况下,需要进行更严格的扫描才能找到分隔符视图。

它依赖于 iOS 在调用之前将单元格添加到表格中heightForFooterInSection:

此外,如上所述,它将删除普通表中的所有分隔符。