如果每个单元格的内容需要大量计算,那么保持UITableView顺畅滚动的最佳方法是什么?例如:
#define maxN 40
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return maxN;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"CellIdentifier";
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:cellId];
//customization
int row = indexPath.row;
int fib = [self fib:row];
cell.textLabel.text = [NSString stringWithFormat:@"%d", fib];
return cell;
}
- (int)fib:(int)n
{
return (n<=2 ? 1 : [self fib:n-1] + [self fib:n-2]);
}
这适用于最大约30的maxN。当值大于该值时,在计算大数字时,表视图将停止。
我知道解决方案与异步计算有关,但您如何设置以保持UI流畅?
更新: 以下是更新的方法。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellId = @"FibIdentifier";
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:cellId];
[self configureCellAtIndexPath:indexPath];
return cell;
}
-(void)configureCellAtIndexPath:(NSIndexPath *)indexPath {
if ([self.fibResults objectAtIndex:indexPath.row] != [NSNull null]) {
// apply cached result
UITableViewCell *cell = [self.fibTable cellForRowAtIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"%d", [(NSNumber*)[self.fibResults objectAtIndex:indexPath.row] intValue]];
return;
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){
NSInteger row = indexPath.row;
int fib = [self fib:row];
//cache the result
[self.fibResults replaceObjectAtIndex:row withObject:[NSNumber numberWithInt:fib]];
dispatch_async(dispatch_get_main_queue(), ^(void){
[self.fibTable reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
});
});
}
好消息是桌子滚动顺畅。坏消息是,单元格中填充了随机值,而不是正确的1,1,2,3,5,8等顺序。
答案 0 :(得分:2)
您在后台线程中执行这些类型的操作,然后调度回主线程以更新表视图单元格。诀窍是那时单元格可能已被重用,因此您需要调用-reloadRowsAtIndexPaths:withRowAnimation:
而不是引用单元格。您还应该将结果缓存在字典或其他形式的memoization中。
-(void)configureCellAtIndexPath:(NSIndexPath *)indexPath {
if (haveCachedResult) {
// apply cached result
return;
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH) ^{
// perform long-running work. Cache result.
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
});
});
}