Hide remove separator line if UITableViewCells are empty [duplicate]
Even simpler than Andrey Z's reply: Simply make a bogus tableFooterView in your UITableView class:
self.tableFooterView = [UIView new]; // to hide empty cells
and Swift:
tableFooterView = UIView()
You can hide UITableView
's standard separator line by using any one of the below snippets of code.
The easiest way to add a custom separator is to add a simple UIView
of 1px height:
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
separatorLineView.backgroundColor = [UIColor clearColor]; // set color as you want.
[cell.contentView addSubview:separatorLineView];
OR
self.tblView=[[UITableView alloc] initWithFrame:CGRectMake(0,0,320,370) style:UITableViewStylePlain];
self.tblView.delegate=self;
self.tblView.dataSource=self;
[self.view addSubview:self.tblView];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
v.backgroundColor = [UIColor clearColor];
[self.tblView setTableHeaderView:v];
[self.tblView setTableFooterView:v];
[v release];
OR
- (float)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
// This will create a "invisible" footer
return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
// To "clear" the footer view
return [[UIView new] autorelease];
}
OR
And also check nickfalk's answer, it is very short and helpful too. And you should also try this single line,
self.tableView.tableFooterView = [[UIView alloc] init];
Not sure but it's working in all the version of iOS that I checked, with iOS 5 and later, up to iOS 7.
Updated answer for swift & iOS 9. This works for tableviews of the .Plain
variety.
tableView.tableFooterView = UIView()