当我设置一个有4行的表格视图时,在填充行的下面仍然有额外的分隔行(或额外的空白单元格)。
如何移除这些单元格?
当我设置一个有4行的表格视图时,在填充行的下面仍然有额外的分隔行(或额外的空白单元格)。
如何移除这些单元格?
当前回答
我只是在ViewDidLoad函数中添加了这一行,问题就解决了。
tableView.tableFooterView = [[UIView alloc] init];
其他回答
如果你只有一个部分,那么最快最简单的方法就是将表格视图样式从“普通”设置为“分组”。(见图片)
如果你有更多的部分,你可能需要将标题高度设置为0(这取决于你/你的客户/你的项目经理的口味)
如果你有更多的部分,并且不想弄乱页眉(即使在最简单的情况下它只是一行),那么你需要设置一个UIView作为页脚,正如它在前面的回答中解释的那样)
试试这个。这招对我很管用:
- (void) viewDidLoad
{
[super viewDidLoad];
// Without ARC
//self.tableView.tableFooterView = [[[UIView alloc] init] autorelease];
// With ARC, tried on Xcode 5
self.tableView.tableFooterView = [UIView new];
}
我想扩展一下我的回答:
简单地添加高度为0的页脚就可以了。(在SDK 4.2, 4.4.1上测试)
- (void) addFooter
{
UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
[self.myTableView setTableFooterView:v];
}
或者更简单——在你设置tableview的地方,添加这一行:
//change height value if extra space is needed at the bottom.
[_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectMake(0,0,0,0)]];
或者更简单——简单地删除任何分隔符:
[_tableView setTableFooterView:[UIView new]];
再次感谢wkw:)
如果你想移除UITableview中不需要的空间,你可以使用下面两个方法
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.1;
}
你可能会找到这个问题的很多答案。大多数都围绕着UITableView的tableFooterView属性进行操作这是隐藏空行的合适方法。为了方便起见,我创建了一个简单的扩展,它允许打开/关闭接口生成器中的空行。 你可以从这个要点文件里查到。我希望它能为你节省一点时间。
extension UITableView {
@IBInspectable
var isEmptyRowsHidden: Bool {
get {
return tableFooterView != nil
}
set {
if newValue {
tableFooterView = UIView(frame: .zero)
} else {
tableFooterView = nil
}
}
}
}
用法:
tableView.isEmptyRowsHidden = true