Display Separator only for the Available CellForRo

2019-02-06 08:53发布

问题:

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?

回答1:

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.



回答2:

Another solution:

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

This works too

self.tableView.tableFooterView = [UIView new];


回答3:

Put this peace of code in your viewDidLoad

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