UITableViewCell content flickers while reloading cells individually

点点圈 提交于 2019-12-05 08:17:35

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.

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)
}

If anyone encountered the same problem:

If you are calculating your cell height and its elements and you are dependent on NSIndexPath for querying any data or conditions, always check that your index path is valid (not nil). Most of UITableView methods that return index path (e.g -(NSIndexPath *)indexPathForCell:.., etc.) may return nil values and will shoot you down an spiral that is debugging views.

In my case, I had a method to determined if my cell should have a header, and for that I checked wether it is the first cell or a condition has changed since previous cell. the problem was indexPath.row == 0 is true even if the index path is actually nil (it's a conceptual problem in Objective-C where nil is 0). so for a brief moment my cells would thought it has a header!

I feel kind of stupid not checking for nil index paths but I think sharing it might help somebody someday.

Subin

In swift am using this and it decreases the flickering

let visibleIndexs = self.ContentTableview.indexPathsForVisibleRows! as NSArray
        print(visibleIndexs)
        for indx in visibleIndexs {
            if self.imgDownloadedIndexs!.containsObject(indx) {
                  UIView.setAnimationsEnabled(false)
                self.ContentTableview.beginUpdates()
                self.ContentTableview.reloadRowsAtIndexPaths([indx as! NSIndexPath], withRowAnimation:.None)
                self.ContentTableview.endUpdates()
                 UIView.setAnimationsEnabled(true)
                self.imgDownloadedIndexs?.removeObject(indx)

            }

Removing rowHeightAtIndexPath method will solve this Issue. make sure you have set some fixed height to the table cell in the story board.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!