当使用普通风格的UITableView有足够多的单元格时,UITableView不能在不滚动的情况下全部显示它们,单元格下方的空白区域没有分隔符。如果我只有几个单元格,它们下面的空白空间包括分隔符。

是否有一种方法可以强制UITableView移除空白区域中的分隔符?如果不是,我将不得不加载一个自定义背景与分隔符绘制在每个单元格,这将使它更难继承的行为。

我在这里发现了一个类似的问题,但我不能在我的实现中使用分组UITableView。


当前回答

我使用以下方法:

UIView *view = [[UIView alloc] init];
myTableView.tableFooterView = view;
[view release];

在viewDidLoad中做。但是你可以把它放在任何地方。

其他回答

如果你使用iOS 7 SDK,这很简单。

只需在你的viewDidLoad方法中添加这一行:

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

迅速:

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

最新的斯威夫特:

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

迅速:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.tableFooterView = UIView()  // it's just 1 line, awesome!
}

使用Daniel的链接,我做了一个扩展,使它更有用:

//UITableViewController+Ext.m
- (void)hideEmptySeparators
{
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
    v.backgroundColor = [UIColor clearColor];
    [self.tableView setTableFooterView:v];
    [v release];
}

经过一些测试,我发现大小可以为0,它也可以工作。所以它不会在表格的末尾添加一些边距。所以感谢wkw的这个黑客。我决定张贴在这里,因为我不喜欢重定向。

斯威夫特版本

最简单的方法是设置tableFooterView属性:

override func viewDidLoad() {
    super.viewDidLoad()
    // This will remove extra separators from tableview
    self.tableView.tableFooterView = UIView(frame: CGRectZero)
}