仅为UITableView中的可用CellForRow显示分隔符 [英] Display Separator only for the Available CellForRow in UITableView

查看:117
本文介绍了仅为UITableView中的可用CellForRow显示分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UITableView和自定义单元格。

工作正常,但问题是UITableView中只有一个或两个单元格。

它也为空单元格提供分隔符。

是否可以仅为使用我的自定义单元格加载的单元格显示分隔符?

I am using UITableView with custom cell.
It is working fine but problem is when there is only one or two cell in UITableView.
It is giving the separator for the empty cell also.
Is it possible to display separator only for the cell that is load with my custom cell?

推荐答案

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

You need to add an empty footer view to hide empty rows from a table.

Swift:

viewDidLoad()

self.tblPeopleList.tableFooterView = UIView.init()

Objective-C:

最简单方式:

viewDidLoad 方法,

self.yourTableView.tableFooterView = [[UIView alloc] initWithFrame : CGRectZero];

self.yourTableView.tableFooterView = [UIView new];

如果你想自定义页脚视图的外观,你可以这样做。

If you want to customize the look of footer view, you can do it like this.

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 {...}

It's the same thing, but here you can add different views for each section if the table has multiple sections. Even you can set different height of each section with this method, - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {...}.

Objective-C答案注释:此答案针对iOS7及以上版本进行测试,对于之前的版本,您需要测试每个案例。
Swift Answer Note :这个答案是针对iOS10.3及更高版本测试的,对于以前的版本你要测试每个案例。

Objective-C Answer Note: This answer is tested for iOS7 and above, for the previous versions you've to test each case. Swift Answer Note: This answer is tested for iOS10.3 and above, for the previous versions you've to test each case.

这篇关于仅为UITableView中的可用CellForRow显示分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆