Summary
Resizing and reloading rows in a tableView where every cell has a dynamic height calculated by constraints causes a lot of other cells to recalculate unnecessarily. How should I solve this?
An example project can be found here.
Long version
I have a view that shows the details of a company and a variable amount of cells (or cards) with more detailed information. I implemented this in a UITableViewController with dynamically sized cells, using auto layout constraints (mostly helped along by this Ray Wenderlich tutorial).
The view has the following cells with variable heights:
- Section 0
- Row 0: Header image
- Row 1: Company information
- Row 2: Buttons
- Section X > 0
- Row 0: About Card
The 'About Card' may or may not have a header image and can be expanded to show the entire text.
Currently I'm having four problems with this:
- The UITableViewController is pushed with
animating: true
. However, the tableView starts out behind the navigation bar and jumps down when it's finished animating. No clue why.
Update This doesn't appear to happen consistently. Joy.
- When doing an action that should reload the buttons to indicate the state change, it reloads the button cell nicely. However, it also causes the company info cell to jump down and animate upwards.
Updating the cell happens with the following code:
func organizationRequestsUpdated() {
self.tableView.reloadSections(NSIndexSet(index: 0), withRowAnimation: UITableViewRowAnimation.None)
}
Tapping an About Card should expand it. I'm currently achieving this by setting
label.numberOfLines = 5
at the cell initiation and using this for toggling the expand cards:@IBAction func tapped(sender: UITapGestureRecognizer) { if let view = sender.view?.superview as? OrganizationDetailAboutCardCell { tableView.beginUpdates() view.toggleReadMore() tableView.endUpdates() } } func toggleReadMore() { let expanded = aboutTextLabel.numberOfLines == 5 aboutTextLabel.numberOfLines = expanded ? 5 : 0 }
I'm pretty sure all of these problems have to do with the automatic sizing of the cells with the constraints. However, I'm currently at a loss on how to fix it. Has anyone done automatic sizing of cells with constraints and updated the cell heights without all the tableView doing weird jumps?
- Popping back to the UITableViewController has most of the cells at the wrong heights. No clue why.
Things I've tried
Problem 2
For problem number 2, I've already tried updating reloading only the button cell;
self.tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 2, inSection: 0)], withRowAnimation: UITableViewRowAnimation.None)
However, this counterintuitively caused all the cells in the section to recalculate incorrectly, ending up with a heap of too small cells. I've also tried this UITableViewRowAnimation.Fade
however, this had the same result.