当我设置一个有4行的表格视图时,在填充行的下面仍然有额外的分隔行(或额外的空白单元格)。
如何移除这些单元格?
当我设置一个有4行的表格视图时,在填充行的下面仍然有额外的分隔行(或额外的空白单元格)。
如何移除这些单元格?
当前回答
在Swift中移除UITableView中空行的额外分隔行
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.yourTableview.tableFooterView = UIView()
}
其他回答
如果你只有一个部分,那么最快最简单的方法就是将表格视图样式从“普通”设置为“分组”。(见图片)
如果你有更多的部分,你可能需要将标题高度设置为0(这取决于你/你的客户/你的项目经理的口味)
如果你有更多的部分,并且不想弄乱页眉(即使在最简单的情况下它只是一行),那么你需要设置一个UIView作为页脚,正如它在前面的回答中解释的那样)
快捷便捷的Swift 4方式。
override func viewDidLoad() {
tableView.tableFooterView = UIView(frame: .zero)
}
如果你有静态单元格。您也可以从检查器窗口关闭分隔符。(如果你需要分隔符,这是不可取的。在这种情况下,使用上述方法)
我知道这个问题已经被接受的答案,但我在这里放了不同的方法,如何隐藏额外的分隔线UITableView。
您可以隐藏tableView的标准分隔线,并在每个单元格的顶部添加您的自定义行。
更新:
添加自定义分隔符最简单的方法是添加高度为1px的简单UIView:
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
separatorLineView.backgroundColor = [UIColor grayColor]; /// may be here is clearColor;
[cell.contentView addSubview:separatorLineView];
OR
self.tblView=[[UITableView alloc] initWithFrame:CGRectMake(0,0,320,370) style:UITableViewStylePlain];
self.tblView.delegate=self;
self.tblView.dataSource=self;
[self.view addSubview:self.tblView];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
v.backgroundColor = [UIColor clearColor];
[self.tblView setTableHeaderView:v];
[self.tblView setTableFooterView:v];
[v release];
OR
- (float)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
// This will create a "invisible" footer
return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
// To "clear" the footer view
return [[UIView new] autorelease];
}
或 我喜欢的最好最简单的方式就是
self.tableView.tableFooterView = [[UIView alloc] init];
试试其中任何一种;
我使用一个表视图来显示固定数量的固定高度行,所以我简单地调整了它的大小并使其不可滚动。
Swift 4.0扩展
只是一个小扩展的故事板:
extension UITableView {
@IBInspectable
var hideSeparatorForEmptyCells: Bool {
set {
tableFooterView = newValue ? UIView() : nil
}
get {
return tableFooterView == nil
}
}
}