我有UITableView。在tableView:cellForRow:atIndexPath:方法中(当数据填充到单元格时),我实现了某种延迟加载。如果rowData中没有键的对象(rowData编号),则NSDictionary程序将在后台启动requestDataForRow:方法。因此,单元格中的数据在单元格变得可见后就会被填充。下面是代码:
static int requestCounter=0;
-(void)requestDataForRow:(NSNumber *)rowIndex
{
requestCounter++;
//NSLog(@"requestDataForRow: %i", [rowIndex intValue]);
PipeListHeavyCellData *cellData=[Database pipeListHeavyCellDataWithJobID:self.jobID andDatabaseIndex:rowIndex];
[rowData setObject:cellData forKey:[NSString stringWithFormat:@"%i", [rowIndex intValue]]];
requestCounter--;
NSLog(@"cellData.number: %@", cellData.number);
if (requestCounter==0)
{
//NSLog(@"reloading pipe table view...");
[self.pipeTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
};
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"pipeCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
[[NSBundle mainBundle] loadNibNamed:@"PipesForJobCell" owner:self options:nil];
cell = pipeCell;
self.pipeCell = nil;
PipeListHeavyCellData *cellData=[[PipeListHeavyCellData alloc] init];
if ([rowData objectForKey:[NSString stringWithFormat:@"%i", indexPath.row]]==nil)
{
//NSLog(@" nil data for row: %i", indexPath.row);
[self performSelectorInBackground:@selector(requestDataForRow:) withObject:[NSNumber numberWithInt:indexPath.row]];
}
else
{
//NSLog(@" has data for row: %i", indexPath.row);
PipeListHeavyCellData *heavyData=[[PipeListHeavyCellData alloc] init];
heavyData=(PipeListHeavyCellData *)[rowData objectForKey:[NSString stringWithFormat:@"%i", indexPath.row]];
cellData._id=[heavyData._id copy];
cellData.number=[heavyData.number copy];
cellData.status=[heavyData.status copy];
};这段代码工作正常,一切都正常,,但是 my表有2000行,如果用户快速地从带有索引10的单元格滚动到使用索引2000的单元格。他必须等待很长时间才能完成所有的数据提取请求(对于第11、12、13、.、2000行),因为当用户滚动表视图时,行变得可见,因此为它们调用了方法requestDataForRow。
我如何优化这些东西?
发布于 2012-05-15 16:58:36
我不得不做些类似的事。您需要创建一个队列,该队列首先处理最近添加的项。
例如,用户打开表并将10个请求排队。将第一个对象排成队列,并开始获取第一行的数据。但是,用户会向下滚动到第31-40行。然后,您必须在队列中的前10行之前插入这些行,因为它们现在具有更高的优先级。关键是不要立即启动10个请求,而是按顺序处理它们。这样,当用户滚动时,您只会“浪费”一个请求--最后一个请求。
实现这一点的一个简单方法是使用[tableView indexPathsForVisibleRows]。
https://stackoverflow.com/questions/10603302
复制相似问题