Jack
Jack

Reputation: 57

Scrolling / static UITableView footer

I want to have a UITableViewCell with a behaviour mixed between a section footer and the table footer:

If the table is not filled (meaning there are not enough cells to fill the entire screen), the cell should stay at the bottom of the table, anchored to the screen edge, leaving blank space between itself and those above. (behaving like a table footer)

If the table is filled, the cell should start scrolling and always remain at the bottom of the table. (behaving like a section footer)

I'd like to avoid as much as possible weird tricks to achieve this, is there an elegant solution that allows me to do this?

Upvotes: 0

Views: 623

Answers (1)

Josh Homann
Josh Homann

Reputation: 16327

Make your footer a separate view on top of the tableView with a constraint to the bottom of the tableView and take an outlet to this constraint. Override scrollViewDidScroll and get the bottom y coordinate of the last visible cell by using tableView.visible cells and calling CGRect.maxy on its frame (if there is no last cell your constraint constant is tableView.frame.size.height - footerView.frames.size.height). Take the difference of the tableView.frame.maxY and the last visible cell's maxY. If the cell is past the tableView.frame.maxY - footerView.frames.size.height then you set your constraint constant to 0, otherwise you set it to the difference.

This has the effect of pinning your footer view to the last visible cell, unless this would force the footer past the bottom of the table, in which case you just pin it to the bottom of the table instead. If there is no last cell you pin the footer to the top of the table.

Upvotes: 1

Related Questions