Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Separator only for the Available CellForRow in 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?

like image 525
KDeogharkar Avatar asked Mar 16 '13 04:03

KDeogharkar


2 Answers

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.

like image 175
Hemang Avatar answered Sep 30 '22 20:09

Hemang


Another solution:

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

This works too

self.tableView.tableFooterView = [UIView new];
like image 30
Fury Avatar answered Sep 30 '22 19:09

Fury