我可以强制UITableView隐藏空单元格之间的分隔符吗?

jes*_*rry 212 cocoa-touch objective-c uitableview ios

当使用具有UITableView足够大的单元格的普通样式时,UITableView如果不滚动则无法显示它们,则单元格下方的空白区域中不会出现分隔符.如果我只有几个单元格,则它们下面的空白区域包含分隔符.

有没有办法可以强制UITableView删除空白区域中的分隔符?如果不是,我将不得不加载自定义背景,并为每个单元格绘制一个分隔符,这将使其更难继承行为.

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

J. *_*sta 220

适用于iOS 7.*和iOS 6.1

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

- (void)viewDidLoad 
{
    [super viewDidLoad];

    // This will remove extra separators from tableview
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
Run Code Online (Sandbox Code Playgroud)

对于以前的版本

你可以将它添加到你的TableViewController(这将适用于任意数量的部分):

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

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

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

    // If you are not using ARC:
    // return [[UIView new] autorelease];
}
Run Code Online (Sandbox Code Playgroud)


Seb*_*ian 138

对于Swift:

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

  • @ayalcinkaya为什么这比'UIView()`更好? (10认同)
  • 更好的用法:tableView.tableFooterView = UIView(frame:CGRect.zero)) (4认同)
  • UIView()等同于UIView(frame:CGRect.zero) (3认同)

Dan*_*per 120

您可以通过为tableview定义页脚来实现所需的功能.有关更多详细信息,请参阅此答案:消除UITableView下面的额外分隔符


Sau*_*eil 67

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

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

经过一些测试,我发现大小可以是0,它也可以.所以它不会在表的末尾添加某种边距.所以,谢谢wkw这个黑客.我决定在这里发帖,因为我不喜欢重定向.


Kin*_*ard 24

Swift版本

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

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

  • 或者只是`self.tableView.tableFooterView = UIView()`(不需要显式框架)。 (2认同)

Seg*_*gev 11

对于Swift:

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

  • 使用CGRect.zero for Swift (5认同)

Sal*_*lmo 8

如果您使用iOS 7 SDK,这非常简单.

只需在viewDidLoad方法中添加以下行:

self.yourTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Run Code Online (Sandbox Code Playgroud)


dun*_*cox 7

将表格设置separatorStyleUITableViewCellSeparatorStyleNone(在代码中或在IB中)应该可以解决问题.

  • 如果我这样做,我将失去所有的分隔符,这不是我正在寻找的行为.如果表没有足够的单元格来填充整个屏幕,则表视图会自动将分隔符绘制到空白区域中,但如果有足够的单元格填充屏幕,则在最后一个单元格下方的空白区域中不会绘制分隔符.我*可以*关闭分隔符,只使用带有分隔符的单元格背景,但随后我将另一个资产添加到我的项目中,这可能会使更新在将来更加困难. (5认同)

Dar*_*eam 5

我使用以下内容:

UIView *view = [[UIView alloc] init];
myTableView.tableFooterView = view;
[view release];
Run Code Online (Sandbox Code Playgroud)

在viewDidLoad中执行此操作.但你可以在任何地方设置它.