UITableViewCell content flickers while reloading cells individually

前端 未结 5 1164
余生分开走
余生分开走 2021-02-08 13:25

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 Auto

相关标签:
5条回答
  • 2021-02-08 13:53

    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)
    }
    
    0 讨论(0)
  • 2021-02-08 13:53

    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)
    
                }
    
    0 讨论(0)
  • 2021-02-08 13:54

    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.

    0 讨论(0)
  • 2021-02-08 13:56

    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.

    0 讨论(0)
  • 2021-02-08 13:58

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

    0 讨论(0)
提交回复
热议问题