Reputation: 1035
Sometimes when table view cells appear in animations, e,g, -[UIView viewWillAppear:animated]
with -[UITableView reloadData]
, the contents inside them pop from top left corner (0, 0) to the final frames. This problem also happens in other view animations. Back in the very old days, this problem did not exist. In one iOS update, the problem appeared but the initial positions were random, usually outside the screen. In another iOS update, the problem became deterministic with the initial position being (0,0).
I have tried [tableView layoutIfNeeded]
, [tableView.layer displayIfNeeded]
after [tableView reloadData]
and those on cells in cellForRowAtIndexPath
. What were I missing?
I uploaded a sample project https://github.com/keithyipkw/ios-animation-bug. To reliably reproduce the problem, set a breakpoint at viewWillAppear, before reloadData. Pull down the screen to show the today center. Run after the today center fully visible.
Upvotes: 4
Views: 781
Reputation: 1035
The fix is to use dequeue and init cells with reuse identifiers. It still works when a cell appear the first time. The magic is to dequeue and init with the same non-null identifier.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
Upvotes: 0
Reputation: 26385
I have the same problem too, this happen when the TV displays cells for the first time.
This is due to the fact that cells are created with the initial size of the prototype in storyboard and later resized on the TV width based size.
To avoid that in cellForRowAtIndexPath
I set the width on the cell to be the size of the TV and force an auto layout update on the cell, to make all elements already fit that size.
I still didn't find a real solution this is more a trick, I hope someone else give a feedback on your question.
Upvotes: 1