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

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

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


将表的separatorStyle设置为UITableViewCellSeparatorStyleNone(在代码中或在IB中)应该可以做到这一点。


你可以通过为tableview定义页脚来实现你想要的。更多细节请看这个答案:消除UITableView下面的额外分隔符


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

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

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


iOS 7。*和iOS 6.1

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

- (void)viewDidLoad 
{
    [super viewDidLoad];

    // This will remove extra separators from tableview
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

对于以前的版本

你可以把这个添加到你的TableViewController(这将适用于任何数量的section):

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
     // This will create a "invisible" footer
     return 0.01f;
 }

如果这还不够,还可以添加以下代码:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{        
    return [UIView new];

    // If you are not using ARC:
    // return [[UIView new] autorelease];
}

我使用以下方法:

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

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


下面的方法对我解决这个问题非常有效:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

CGRect frame = [self.view frame];
frame.size.height =  frame.size.height - (kTableRowHeight * numberOfRowsInTable);

UIView *footerView = [[UIView alloc] initWithFrame:frame];
return footerView; }

其中kTableRowHeight是行单元格的高度,numberOfRowsInTable是表中的行数。

希望有帮助,

Brenton。


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

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

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

迅速:

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

最新的斯威夫特:

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

斯威夫特版本

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

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

迅速:

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