Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 671
Mustafa Avatar asked Mar 12 '14 09:03

Mustafa


2 Answers

Try to set footerView.autoresizingMask = .flexibleWidth

Example code:

let footerView = Bundle.main.loadNibNamed(String(describing: MyFooterView.self), owner: nil, options: nil)?.first as! MyFooterView
footerView.frame = CGRect(origin: .zero, size: CGSize(width: view.bounds.width, height: 80))
footerView.autoresizingMask = .flexibleWidth
tableView.tableFooterView = footerView
like image 79
Yakiv Kovalskyi Avatar answered Sep 27 '22 19:09

Yakiv Kovalskyi


Looks like for now estimatedHeightForRowAtIndexPath: messes with footer views:

https://twitter.com/steipete/status/420538600384888832

like image 27
Ben Jackson Avatar answered Sep 27 '22 19:09

Ben Jackson