Can I force a UITableView to hide the separator between empty cells?

jessecurry picture jessecurry · Oct 27, 2009 · Viewed 126k times · Source

When using a plain-style UITableView with a large enough number of cells that the UITableView cannot display them all without scrolling, no separators appear in the empty space below the cells. If I have only a few cells the empty space below them includes separators.

Is there a way that I can force a UITableView to remove the separators in the empty space? If not I'll have to load a custom background with a separator drawn in for each cell which will make it harder to inherit behavior.

I found a somewhat similar question here, but I can't use a grouped UITableView in my implementation.

Answer

J. Costa picture J. Costa · Mar 21, 2011

For iOS 7.* and iOS 6.1

The easiest method is to set the tableFooterView property:

- (void)viewDidLoad 
{
    [super viewDidLoad];

    // This will remove extra separators from tableview
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

For previous versions

You could add this to your TableViewController (this will work for any number of sections):

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
     // This will create a "invisible" footer
     return 0.01f;
 }

and if it is not enough, add the following code too:

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

    // If you are not using ARC:
    // return [[UIView new] autorelease];
}