这个问题已经在这里有一个答案:
- 下面,消除多余的UITableView分隔 33回答
当使用普通样式UITableView
有足够多的细胞UITableView
不能完全不需要滚动显示出来,没有分隔符出现在单元下方的空白区域。 如果我只有少数细胞在它们下面的空白处包括分隔符。
有没有一种方法,我可以强制UITableView
去除空的空间分隔? 如果不是我得加载与每个单元,这将使其难以继承行为绘制在分离自定义背景。
我发现了一个有点类似的问题在这里 ,但我不能用一个分组UITableView
在我的实现。
你可以实现你通过定义实现代码如下页脚想要什么。 看到这个答案的详细信息: 消除下面的UITableView多余的分隔符
对于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(这会为任何数量的部分工作):
- (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];
}
对于斯威夫特:
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView() // it's just 1 line, awesome!
}
使用从丹尼尔的链接,我做了一个扩展,使它更可用:
//UITableViewController+Ext.m
- (void)hideEmptySeparators
{
UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
v.backgroundColor = [UIColor clearColor];
[self.tableView setTableFooterView:v];
[v release];
}
一些testings后,我发现大小可以是0和它的作品也是如此。 所以它不会在表的末尾添加某种保证金。 所以感谢WKW这个黑客。 我决定张贴在这里,因为我不喜欢重定向。
斯威夫特版本
最简单的方法是设置tableFooterView属性:
override func viewDidLoad() {
super.viewDidLoad()
// This will remove extra separators from tableview
self.tableView.tableFooterView = UIView(frame: CGRectZero)
}
对于斯威夫特:
self.tableView.tableFooterView = UIView(frame: CGRectZero)
如果您使用的iOS 7 SDK,这是非常简单的。
只需添加这条线在您的viewDidLoad方法:
self.yourTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
设置表的separatorStyle
到UITableViewCellSeparatorStyleNone
(代码或IB)应该做的伎俩。
我用的是以下几点:
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是行我在表的数量。
希望帮助,
布伦顿。
文章来源: Can I force a UITableView to hide the separator between empty cells? [duplicate]