仅在UITableView中显示可用CellForRow的分隔符
我正在使用自定义单元格的UITableView。
它工作正常,但问题是当UITableView中只有一个或两个单元格。
它也给空单元的分隔符。
是否有可能显示分隔符只有与我的自定义单元格加载的单元格?
您需要添加一个空的页脚视图来隐藏表中的空行。
Swift 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答案注意 :这个答案已经过iOS7及以上版本的testing,以前的版本要testing每种情况。 Swift答案注意 :这个答案已经过testing,适用于iOS10.3及以上版本,以前的版本要testing每种情况。
另一个scheme
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];