Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell content flickers while reloading cells individually

I have a table view linked to a NSFetchedResultController (i.e. loading data and also tracking changes to data is bound to the FRC)

I'm not using AutoLayout in my cells (due to huge performance drop it introduces in iOS 8). I'm laying out my cells' content manually in cells (using -(void)layoutSubviews). plus, the height of rows are calculated based on content and are cached/invalidated properly.

If any condition related to my data changes, related cells get updated individually (with the whole -(void)controllerWillChangeContent:... through -(void)controllerDidChangeContent:... delegate methods implemented) the animation for row updates is: UITableViewRowAnimationNone

The problem here is that, My cells have transparent backgrounds, and I can see some visual noise (most likely the actual cell getting stretched vertically, I can't say for sure because they are really transient and short lived) during [self.tableView reloadRowsAtIndexPaths:...];.

I have tried many things to no avail!

Here are the things I have tried that doesn't work:

  • Implementing -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath, returning the accurate value for estimated heights.
  • setting self.tableView.rowHeight=UITableViewAutomaticDimension;, helps a lot but doesn't fix it.
  • Disabling animation upon [self.tableView beginUpdates]; and enabling after [self.tableView endUpdates];
  • Removing all subviews from cells' content in -(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

Things that help a little:

  • Making Cells opaque (i.e. setting a background color for cells). weirdly enough, the artifacts seems to be under the actual cell content.

More Info

  • Most of my cells' content are nodes from AsyncDisplayKit (ASTextNode, ASImageNode), but replacing them with their UIKit counterparts doesn't solve the problem.
  • All my visual updates are happening on main thread. I always make sure of that.
  • The problem is not ubiquitous, but exist more often than not. They seem to happen more after second cells inserted if it helps.
  • Logging the method -(void)layoutSubviews in my cells shows that the cells with update condition get reloaded 3 times in a row upon each update in contrast with other cells getting updated just the once. I'm not forcing any [cell layoutIfNeeded]; or [cell setNeedsLayout]; anywhere.
  • Reloading the whole table view using [self.tableView reloadData]; is not an option for me.

Additional More Info

  • Making cells opaque leads to cells getting laid out just the once! weird!

At the end, I'm sorry I can't share any actual code, because it's not a test project and the complexity of the implementations renders any code sharing futile unless comprehended as a whole.

Thanks for your time.

like image 513
M. Porooshani Avatar asked Apr 20 '15 05:04

M. Porooshani


2 Answers

I was also facing same problem of flickering UITableViewCell while using reloadRowsAtIndexPath, so what I did was -

     [UIView setAnimationsEnabled:NO];
     [self.tableView beginUpdates];

     [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]
                                               withRowAnimation:UITableViewRowAnimationNone];
     [self.tableView endUpdates];
     [UIView setAnimationsEnabled:YES];

The above snippet will have no animation on cells while reloading the cell.

like image 57
Sandy Avatar answered Oct 28 '22 03:10

Sandy


Yeah, you can achieve a similar result from the @Sandy's answer using performWithoutAnimation (Swift Version)

UIView.performWithoutAnimation {
      self.tableView.reloadRows(at: [indexPath], with: .none)
}
like image 19
Antonio Junior Avatar answered Oct 28 '22 04:10

Antonio Junior