Search code examples
iosuitableviewautolayouttablefooterview

tableFooterView not aligning itself properly at the bottom of the table


I'm experiencing a strange behavior with UITableView. I've setup a UIViewController with a UITableView. UITableView contains a tableHeaderView, tableFooterView, a section footer view, and a couple of UITableViewCells with dynamic heights.

The problem is that the tableFooterView appears somewhere in the middle of the table (hovering over the cell), instead of aligning itself at the bottom of the table's content. Apparently, table's contentSize property is also returning incorrect content size (specifically the height).

The custom section footer view, however, is appearing in the right place (and below the actual tableFooterView -- which is hovering in the middle of the table).

Any ideas what's going on?

Note: My custom table-view (with dynamic height handing using Auto Layouts) is showing a "Ambiguous Layout: 2 views are vertically ambiguous." warning, but it's resizing correctly at run-time.

The table setup is pretty simple:

// Set table header/footer view
self.myTableView.tableHeaderView = self.myTableHeaderView;
self.mainTableView.tableFooterView = self.myTableFooterView;

// Table delegate methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self heightForCellAtIndexPath:indexPath];
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [MyCustomCell defaultHeight];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

    if (section == 0) {
        return self.mySectionFooterView;
    }

    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    if (section == 0) {
        return [self heightForSectionFooterView];
    }

    return 0.01f;
}

Solution

  • I ended up using an extra section with just the header and footer (and no rows), to get the desired results. Maybe the auto-layouts are acting weird. I'm still getting the "Ambiguous Layout: 2 views are vertically ambiguous.", but the UI looks good.