0

I have an app with a UITableViewController, initially with a single section with a single row (that says "Loading..." in it). It fetches some data from the internet, at which point it makes the following call:

[tableView reloadSections:[NSIndexPath indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];

This is necessary because after fetching the data, there's now a lot of rows, rather than just the 1 there was initially (so asking the table view for the list of visible cells isn't a possibility, since it just returns the 1 old one)

The trouble is that it's then calling the cellForRowAtIndexPath delegate method for every single row, not just the visible ones. I've verified that by putting some logging into the method. If I switch the line to a simple [tableView reloadData] then it only calls cellForRowAtIndexPath for the first few rows, until the point where you scroll and others come into view. This happens on my iOS4 iPod Touch and also in the iOS5 simulator.

I have about 300 rows and it's making it very slow to load and usually crashes on my iPod Touch.

The reason I'm not just using reloadData is that I want the addition of the new rows to be animated, whereas reloadData just makes them suddenly appear.

4

2 回答 2

1

你可以这样做:

[tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows] 
                 withRowAnimation:UITableViewRowAnimationNone]; 
于 2012-04-24T15:37:46.020 回答
0

这就是我用来向表中添加行的方法:

NSMutableArray *indexPathsToInsert = [[NSMutableArray alloc] init];

    for (NSInteger i = previousTableDataCount; i < [tableData count]; i++) {
        [indexPathsToInsert addObject:[NSIndexPath indexPathForRow:i inSection:0]];
    }

    // Apply the updates.
    [tableView beginUpdates];

    [tableView insertRowsAtIndexPaths:indexPathsToInsert withRowAnimation:UITableViewRowAnimationNone];
    [tableView endUpdates];

它不会重新加载所有数据,它只是在现有 tableView 的底部插入新行。tableData 数组是表的数据源数组,previousTableDataCount 是 tableData 数组在插入来自 Internet 的新数据之前所具有的项目的索引...

于 2012-04-24T16:37:32.107 回答