12

我正在使用带有自定义单元格的 UITableView。
它工作正常,但问题是 UITableView 中只有一两个单元格时。
它也为空单元格提供分隔符。
是否可以仅为使用我的自定义单元格加载的单元格显示分隔符?

4

3 回答 3

26

您需要添加一个空的页脚视图来隐藏表格中的空行。

迅速:

在你的viewDidLoad()

self.tblPeopleList.tableFooterView = UIView.init()

目标-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 及更高版本进行了测试,对于以前的版本,您必须测试每种情况。 Swift 回答注意:这个答案是针对 iOS10.3 及更高版本进行测试的,对于以前的版本,您必须测试每种情况。

于 2013-03-16T05:01:36.840 回答
7

另一种解决方案:

UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
v.backgroundColor = [UIColor clearColor];
[self.tableView setTableFooterView:v];

这也有效

self.tableView.tableFooterView = [UIView new];
于 2013-12-18T03:22:52.790 回答
2

把这个和平的代码放在你的 viewDidLoad

self.tblTest.tableFooterView = [[UIView alloc] initWithFrame : CGRectZero];
于 2014-12-12T06:50:14.963 回答