how to hide empty rows in a UITableView and change the height of the Uitableview based on non-empty rows
Solution 1:
NEW ANSWER
In Swift 2.2, 3.0 and onwards, do the following:
tableView.tableFooterView = UIView()
OLD ANSWER BELOW. KEPT FOR POSTERITY.
If you must use UITableViewStylePlain
, and you don't use a footerView for anything else, you can use the following semi-dirty solution if you have ARC enabled.:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] init];
return view;
}
If you have ARC disabled, use the following:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[[UIView alloc] init] autorelease];
return view;
}
This creates an invisible footerView, that appears immediately after the last data-filled cell.
Solution 2:
You sure you are using UITableViewStyleGrouped
style?
Because by default it is set to UITableViewStylePlain
which shows empty cells too after displaying filled cells.
Solution 3:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.01f;
}
and for iOS 7
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Solution 4:
I solved the problem by creating a simple UIView as footer view with the same background color as the table background:
(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)] autorelease];
view.backgroundColor = [UIColor whiteColor];
return view;
}
Maybe you have to disable scrolling in your table view in addition.
Solution 5:
If your tableview has multiple sections, you may try using this:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if (section == tableView.numberOfSections - 1) {
return [UIView new];
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (section == tableView.numberOfSections - 1) {
return 1;
}
return 0;
}