InsertRowsAtIndexPaths calling cellForRowAtIndexPath for each row

I am trying to insert a bunch of rows into an empty UITableView in one step using insertRowsAtIndexPaths. (I know this doesn't sound so useful, but it's a simplified version of what I really need to do.)

The problem that I see is that after making an insertRowsAtIndexPaths call, I get cellForRowAtIndexPath calls for every inserted row, not just the ones that are visible.

This does not seem right. And it is almost useless to me if that is so.

The only slightly strange other artifact that I see is that when I do this, it seems to revitalize the ranks in place. This is not a withRowAnimation parameter, as it is not set. But there seems to be some kind of higher-level animation concept here. I had the idea that since she was animating the lines in place, she believed that for more / all lines, cells were needed until they were pushed off the screen. But I can't turn off this animation without just using reloadData, which I would rather avoid.

Typically, I have a dataset that changes behind the scenes. I can carefully go through and build the changes to generate data for insert / delete / reload calls. But I can’t limit it to the fact that the screen since then does not match the data provider.

I suppose I should be missing something ... any ideas?

(I do this in a pair of beginUpdates / endUpdates, but that doesn't make any difference.)

+3
source share
8 answers

Today I spoke with some Apple people. This issue is definitely related to the animation that the UITableView does when inserting new rows. This is an animation that creates new rows for cells, separate from the animation that is used when these rows are inserted through insertRowsAtIndexPaths.

Since table-level animations create new rows using a kind of “growing” type, during insertion animations, small numbers of many cells are considered visible, and the table will call cellForRowAtIndexPath for them.

, , . - "": , . , , cellForRowAtIndexPath.

"" , "" . , , .

+5

, , bMassiveLoad, false. " ", true, , cellForRowAtIndexPath, . "maseive load" false reloadData...

, , .

+2

, :

, pocjoc

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
    UITableView *tableView = self.tableView;
    switch(type) {
        case NSFetchedResultsChangeInsert:
            massiveLoad = YES;
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        ...
    }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView endUpdates];
    if (massiveLoad) {
        massiveLoad = NO;
        [self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationFade];
    }
}
+2

UITableView insertRowsAtIndexPaths . , YES NO.

.

, tableView , [tableView reloadData].

0

, , , (, 10 , , tableView 10 ), , cellForRowAtIndexPath.

, . . tableView, . . , , (.. 10 50 , , 40, 10 ). , , 10 . :

NSArray *indexPaths = [myTableView indexPathsForVisibleCells];
[myTableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];

10 , ( , ). cellForRowAtIndexPath, :

if (![myDataArray count]) {
    // create a blank cell;
} else {
    // add context to your cells as you want;
}

,

0

, , UITableView : beginUpdates endUpdates (), ( , , cellForRowAtIndexPath).

, , , , ...

0

self.mainTableView.beginUpdates()
    self.mainTableView.insertRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.Fade)
    self.mainTableView.reloadRowsAtIndexPaths(self.mainTableView.indexPathsForVisibleRows!, withRowAnimation: UITableViewRowAnimation.Fade)
    self.mainTableView.endUpdates()
0

UITableView queries visible cells using a method cellForRowAtIndexPath:and can shorten processing time when there are many cells, ex 1k cells, and reduce wasted memory with a reuse mechanism. If you need to process all the cells, then you must implement your own method to reprocess the original data, and then call [tableView reloadData]to update the user interface.

-1
source

All Articles