reload uitableview with new data caused flickering

后端 未结 9 1053
Happy的楠姐
Happy的楠姐 2020-12-16 11:55

I added an infinite scrolling feature and realized that whenever I reload the uitableview, the view flickers..I am not sure how to fix the flickering at this point. Please h

相关标签:
9条回答
  • 2020-12-16 12:23

    As you have guessed, flickering is caused by calling [self.tableView reloadData]; rapidly and repeatedly, especially on iOS 5.x devices. But you probably do not want to reload the entire table, you want to update just the view within the visible cells of the table.

    Let's say you want to update each cell of a table to reflect the latest download % as a file is downloading. A method [downloading:totalRead:totalExpected:] gets called in my example, extremely rapidly as bytes are downloading.

    This is what NOT to do... reload the table on every little update (in this example, the developer may rely on "cellForRowAtIndexPath" or "willDisplayCell" methods perform the updating of all the visible cells):

        - (void)downloading:(PPFile *)file totalRead:(long long)read totalExpected:(long long)expected {
            // Bad! Causes flickering when rapidly executed:
            [self.tableView reloadData];
        }
    

    The following is a better solution. When a cell's view needs to be updated, find that cell by grabbing only the visible cells from the table, and then update that cell's view directly without reloading the table:

        - (void)downloading:(PPFile *)file totalRead:(long long)read totalExpected:(long long)expected {
            NSArray *cells = [self.tableView visibleCells];
    
            for(MYCellView *cell in cells) {
                if(cell.fileReference == file) {
                    // Update your cell's view here.
                }
            }
        }
    

    EDIT: The docs recommend the same:

    Calling this method causes the table view to ask its data source for new cells for the specified sections. The table view animates the insertion of new cells in as it animates the old cells out. Call this method if you want to alert the user that the values of the designated sections are changing. If, however, you just want to change values in cells of the specified sections without alerting the user, you can get those cells and directly set their new values.

    0 讨论(0)
  • 2020-12-16 12:23

    Refer your cellForRowAtIndexPath delegate method. You might be doing some operation that might cause a flick. In my case, I was setting an image of a image view with animation. So whenever I reload the table, it was flickering due to that animation.

    I removed it and worked for me..

    0 讨论(0)
  • 2020-12-16 12:24

    Why don't you simply call reloadSections method instead of [self.tableView reloadData];

    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
    
    0 讨论(0)
  • 2020-12-16 12:32
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationNone];
    

    whenever you remove or add a table row in updates..the table view reloads and ask the datasource for the number of rows based on which cell is animated to position.

    you are removing row from table but not from datasource..so rows are deleted but your datasource still points out that no row is deleted..20 objects are still there. your data source will be something like

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return MyDataModel.count; // 
    }
    

    so you need to remove or add new data in datasource also..something like this

       NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:0];
       [MyDataModel removeObjectAtIndex:index.row];
       [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationNone];
    
    0 讨论(0)
  • 2020-12-16 12:32

    Yes, try this and you are done!!

    UIView.performWithoutAnimation {
      self.tableView.reloadRows(at: [indexPath], with: .none)
    

    }

    Happy coding !!

    0 讨论(0)
  • 2020-12-16 12:42

    what work in my project was :

    UIView.PerformWithoutAnimation(() =>
    {
    tableView.ReloadData();
    tableView.LayoutIfNeeded();
    });
    

    (it's in c# xamarin...but the same with swift of objective c)

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