Can I force a UITableView to hide the separator between empty cells?
IosObjective CCocoa TouchUitableviewIos Problem Overview
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.
Ios Solutions
Solution 1 - Ios
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];
}
Solution 2 - Ios
For Swift:
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView() // it's just 1 line, awesome!
}
Solution 3 - Ios
You can achieve what you want by defining a footer for the tableview. See this answer for more details:https://stackoverflow.com/questions/1369831/eliminate-extra-separators-below-uitableview-in-iphone-sdk">Eliminate Extra separators below UITableView
Solution 4 - Ios
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.
Solution 5 - Ios
Swift Version
The easiest method is to set the tableFooterView property:
override func viewDidLoad() {
super.viewDidLoad()
// This will remove extra separators from tableview
self.tableView.tableFooterView = UIView(frame: CGRectZero)
}
Solution 6 - Ios
For Swift:
self.tableView.tableFooterView = UIView(frame: CGRectZero)
For newest Swift:
self.tableView.tableFooterView = UIView(frame: CGRect.zero)
Solution 7 - Ios
If you use iOS 7 SDK, this is very simple.
Just add this line in your viewDidLoad method:
self.yourTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Solution 8 - Ios
Setting the table's separatorStyle
to UITableViewCellSeparatorStyleNone
(in code or in IB) should do the trick.
Solution 9 - Ios
I use the following:
UIView *view = [[UIView alloc] init];
myTableView.tableFooterView = view;
[view release];
Doing it in viewDidLoad. But you can set it anywhere.
Solution 10 - Ios
The following worked very well for me for this problem:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
CGRect frame = [self.view frame];
frame.size.height = frame.size.height - (kTableRowHeight * numberOfRowsInTable);
UIView *footerView = [[UIView alloc] initWithFrame:frame];
return footerView; }
Where kTableRowHeight is the height of my row cells and numberOfRowsInTable is the number of rows I had in the table.
Hope that helps,
Brenton.