views:

2222

answers:

5

When using a plain-style UITableView with a large enough number of cells that the table view 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 table view in my implementation.

A: 

Setting the table's separatorStyle to UITableViewCellSeparatorStyleNone (in code or in IB) should do the trick.

duncanwilcox
If I do that I'll lose all of the separators, which is not the behavior that I am looking for.The table view automatically draws separators into the empty space if the table does not have enough cells to fill one full screen, but if there are enough cells to fill a screen there are no separators drawn in the empty space below the last cell.I *could* turn off the separators and just use a cell background with a separator drawn in, but then I am adding another asset to my project which may make updating more difficult in the future.
jessecurry
I see. I don't believe you can remove separators only where no cells are present. The only other simple solution that comes to mind is to have your cell class inherit from a very minimal UITableViewCell subclass that only draws the separator line. It is an additional piece of complexity, but doesn't need the separate image.
duncanwilcox
A: 

It seems that there is no way to achieve the behavior that I would like. I'll need to resort to hiding the separators entirely and just drawing a separator on the bottom of each cell that I create.

jessecurry
+3  A: 

You can achieve what you want by defining a footer for the tableview. See this answer for more details: http://stackoverflow.com/questions/1369831/eliminate-extra-separators-below-uitableview-in-iphone-sdk

Daniel Hepper
thanks, that's just what I was looking for.
jessecurry
+3  A: 

Using the link from Daniel, I made an extension to make it more usable:

//UITableViewController+Ext.m
- (void)hideEmptySeparators
{
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
    v.backgroundColor = [UIColor clearColor];
    [self.tableView setTableFooterView:v];
    [v release];
}

After some testings, I found out that the size can be 0 and it works as well. So it doesn't add some kind of margin at the end of the table. So thanks wkw for this hack. I decided to post that here since I don't like redirect.

AngeDeLaMort
A: 

@AngeDeLaMort

Thanks! Solution worked for me.

Jeremy