Ios UITableView重载部分导致为所有行调用cellForRowAtIndexPath
我有一个带有Ios UITableView重载部分导致为所有行调用cellForRowAtIndexPath,ios,uitableview,Ios,Uitableview,我有一个带有UITableViewController的应用程序,最初只有一个部分和一行(上面写着“加载…”)。它从internet获取一些数据,并在此时进行以下调用: [tableView reloadSections:[NSIndexPath indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade]; 这是必要的,因为在获取数据后,现在有很多行,而不仅仅是最初的1行(因此不可能向表视图请求可见单元格列表,因为它只返
UITableViewController
的应用程序,最初只有一个部分和一行(上面写着“加载…”)。它从internet获取一些数据,并在此时进行以下调用:
[tableView reloadSections:[NSIndexPath indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
这是必要的,因为在获取数据后,现在有很多行,而不仅仅是最初的1行(因此不可能向表视图请求可见单元格列表,因为它只返回1个旧的)
问题在于,它会为每一行调用cellforrowatinexpath
delegate方法,而不仅仅是对可见的行。我通过在方法中添加一些日志来验证这一点。如果我将行切换到一个简单的[tableView reloadData]
,那么它只对前几行调用cellforrowatinedexpath
,直到您滚动并看到其他行为止。这发生在我的iOS4 iPod Touch和iOS5模拟器上
我有大约300行,这使得它的加载速度非常慢,通常会在我的iPodtouch上崩溃
我不只是使用reloadData
的原因是我希望新行的添加是动画的,而reloadData
只是让它们突然出现。您可以这样做:
[tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows]
withRowAnimation:UITableViewRowAnimationNone];
这是我用于将行添加到表中的内容:
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];
NSMutableArray*indexPathsToInsert=[[NSMutableArray alloc]init];
对于(NSInteger i=previousTableDataCount;i<[tableData count];i++){
[indexPathsToInsert addObject:[NSIndexPath indexPathForRow:i第0节]];
}
//应用更新。
[tableView开始更新];
[tableView InsertRowsAndExpaths:InExpathStoInsertWithRowAnimation:UITableViewRowAnimationNone];
[tableView EndUpdate];
它不会重新加载所有数据,只是在现有tableView的底部插入新行。
tableData数组是表的数据源数组,而previousTableDataCount是包含tableData数组在从internet插入新数据之前所拥有的项的索引…我编辑了我的问题,并澄清了一点:
indExpathForVisibleRows
不是一个选项,因为基础数据已更改,它只返回一个旧的单元格(它不包括任何应该添加的新行)好吧,你说得对,那就不是一个选项;)但是我认为,如果您调用[NSIndexPath indexSetWithIndex:0],tableView必须加载所有单元格/数据,因为tableView怎么知道在您提供数据的末尾是否没有第0节的数据。但是如果是这样的话,为什么reloadData
足够聪明来正确处理它呢?好问题,我可能错了,但我猜。reloadData首先调用numberOfCells,以便它知道现在有多行。然后是IndExpathForVisibleRows。。。只是猜测;)我不确定这是否会有帮助-鉴于表格以一行开始,这基本上仍然需要加载所有单元格,对吗?