显示分隔符仅适用于UITableView中的可用CellForRow
问题描述:
我正在使用UITableView与自定义单元格。
它工作正常,但问题是当UITableView中只有一个或两个单元时。
它也给空单元的分隔符。
是否可以仅显示使用我的自定义单元格加载的单元格的分隔符?显示分隔符仅适用于UITableView中的可用CellForRow
答
您需要添加一个空的页脚视图来隐藏表中的空行。
斯威夫特3.X:
在你viewDidLoad()
self.tblPeopleList.tableFooterView = UIView.init()
的Objective-C:
最简单的方法:
在viewDidLoad
方法,
self.yourTableView.tableFooterView = [[UIView alloc] initWithFrame : CGRectZero];
或
self.yourTableView.tableFooterView = [UIView new];
或
如果您想自定义页脚视图的外观,你能做到这样。
UIView *view = [[UIView alloc] initWithFrame:self.view.bounds];
view.backgroundColor = [UIColor redColor];
self.yourTableView.tableFooterView = view;
//OR add an image in footer
//UIImageView *imageView = [[UIImageView alloc] initWithImage:footerImage.png]
//imageView.frame = table.frame;
//self.yourTableView.tableFooterView = imageView;
另一种方式:
实现一个表的数据源的方法,
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return [UIView new];
}
这是同样的事情,但在这里你可以为每个部分添加不同的看法,如果表中有多个部分。即使你可以用这种方法设置不同高度的每个部分,- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {...}
。
Objective-C Answer Note:这个答案已经过iOS7及以上版本测试,以前的版本要测试每种情况。Swift Answer Note:这个答案已经过测试iOS10.3及以上版本,以前的版本你要测试每种情况。
答
另一种解决方案:
UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
v.backgroundColor = [UIColor clearColor];
[self.tableView setTableFooterView:v];
这工作太
self.tableView.tableFooterView = [UIView new];
答
把这个代码和平在viewDidLoad中
self.tblTest.tableFooterView = [[UIView alloc] initWithFrame : CGRectZero];
此代码工作完全在双方的iOS 7和8。您不需要清除背景视图的颜色。 – Jemythehigh 2014-12-12 06:54:23