Display Separator only for the Available CellForRow in UITableView

时光怂恿深爱的人放手 提交于 2019-11-30 08:47:25

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

Swift:

In your viewDidLoad()

self.tblPeopleList.tableFooterView = UIView.init()

Objective-C:

Easiest way:

in your viewDidLoad method,

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

or

self.yourTableView.tableFooterView = [UIView new];

or

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;

Another way:

Implement data source method of a table,

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

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

Another solution:

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

This works too

self.tableView.tableFooterView = [UIView new];
ManishRathore

Put this peace of code in your viewDidLoad

self.tblTest.tableFooterView = [[UIView alloc] initWithFrame : CGRectZero];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!