当我设置一个有4行的表格视图时,在填充行的下面仍然有额外的分隔行(或额外的空白单元格)。

如何移除这些单元格?


当前回答

我很幸运地实现了一个被接受的答案(iOS 9+, Swift 2.2)。我尝试着执行:

self.tableView.tableFooterView = UIView(frame: .zero)

然而,对我的tableView没有任何影响-我相信这可能与我使用UITableViewController的事实有关。

相反,我只需要重写viewForFooterInSection方法(我没有在其他地方设置tableFooterView):

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    return UIView(frame: .zero)
}

这对于具有单个section的tableView来说效果很好(如果有多个section,则需要指定最后一个)。

其他回答

Uitableview额外的分隔线隐藏额外的分隔线隐藏在swift 3.0

 self.tbltableView.tableFooterView = UIView(frame: .zero)

Swift 4.0扩展

只是一个小扩展的故事板:

extension UITableView {
    @IBInspectable
    var hideSeparatorForEmptyCells: Bool {
        set {
            tableFooterView = newValue ? UIView() : nil
        }
        get {
            return tableFooterView == nil
        }
    }
}

如果你只有一个部分,那么最快最简单的方法就是将表格视图样式从“普通”设置为“分组”。(见图片)

如果你有更多的部分,你可能需要将标题高度设置为0(这取决于你/你的客户/你的项目经理的口味)

如果你有更多的部分,并且不想弄乱页眉(即使在最简单的情况下它只是一行),那么你需要设置一个UIView作为页脚,正如它在前面的回答中解释的那样)

如果你要子类化UITableView,你需要这样做…

-(void)didMoveToSuperview {
    [super didMoveToSuperview];
    self.tableFooterView = [UIView new];
}

这里有另一种不使用分组表样式的方法,一种您可能猜不到的方法。向表中添加页眉和页脚(也许一个或另一个就足够了,还没有选中)会导致分隔符从填充行/空白行中消失。

我无意中发现了这一点,因为我想在桌子的顶部和底部留出一点空间,以减少触碰按钮的风险,而不是手指肥大的表格单元格。下面是一个将空白视图作为页眉和页脚插入的方法。使用您喜欢的任何高度,您仍然可以消除额外的分隔线。

- (void) addHeaderAndFooter
{
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
    v.backgroundColor = [UIColor clearColor];
    [self.myTableView setTableHeaderView:v];
    [self.myTableView setTableFooterView:v];
    [v release];
}

In response to @Casebash, I went back to the code in my app ("AcmeLists" List Manager in iTunes store...) and short-circuited the addHeaderAndFooter method to verify. Without it, I have the extra row separators; with the code, I have what you see in this window snap: no table row separators picture. So I'm not sure why it wouldn't have worked for you. Moreover, it makes sense to me that having any custom footer on a table view would necessarily have to stop drawing row separators for blank rows below it. That would be hideous. For reference, I looked at tables where there were more rows than could be viewed on screen, and then for a table with two rows. In both cases, no extraneous separators.

也许您的自定义视图实际上并没有添加。要检查这一点,将背景颜色设置为clearColor以外的颜色,例如,[UIColor redColor]。如果您在表格底部没有看到一些红色条,则表示您的页脚没有设置。