My programmatically generated custom UITableViewCell
contains several UILabel
that can wrap the text:
newLabel.numberOfLines = 0;
newLabel.lineBreakMode = NSLineBreakByWordWrapping;
I use these cells in a UITableView
with the following parameters:
self.tableView.estimatedRowHeight = 120.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
I therefore expect that the height of cells is automatically adjusted and the UILabel
show multiple lines when required.
Because this did not work for newly created cells, I force the UITableView
to perform a update in viewDidAppear
:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (self.appearsFirstTime) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView beginUpdates];
[self.tableView endUpdates];
self.appearsFirstTime = NO;
});
}
}
This works well for the cells that were generated during the first appearance of the UITableView
and thus already existed when updating. However, it does not work for cells which were generated by scrolling at a later time. These later initialized cells have no chance to benefit from the update, and therefore have an incorrect height.
I have found a different approach to solve the problem: https://stackoverflow.com/a/25967370/4943710 But this approach suffers from the fact that the layout process may require several passes. The approach is then working with interim results, which lead to incorrect heights. The result again is missing rows of text. In addition, it seems that the cells lose the ability to adapt in later updates (for example, when changing the interface orientation).
Now my question:
How can I make sure that cells generated later (after updating the UITableView
) always will be displayed with correct height?