我是第一次在真实设备上进行测试,在解决了一些明显的性能问题后,我一直在研究如何实现平滑滚动。
这是我的做法:
- 数据在sqlite中
- 我有一个带有标题的小数组
- 我在每个 header 数组中都有来自 Db 的 ID 列表
例如
标题A IDs= 1,2; header B IDs= 3,4
- 我延迟加载单元格和对象以获取数据
- 一次加载不到 10 个项目
- 加载速度很快,唯一的问题是滚动
这是加载单元格的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"ProductCell";
ProductCell *cell = (ProductCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ProductCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// Set up the cell...
Product *p = [self locateRecord:indexPath];
cell.nameLabel.text = [p.name capitalizedString];
cell.codeLabel.text = p.ref;
if ([self.selectedProducts objectForKey:[NSNumber numberWithInt:p.Id]]) {
OrderDetail *d = [self findDetail:p];
cell.qty.text = [NSString stringWithFormat:@"%ld",[d.qty integerValue]];
}
return cell;
}
- (id) locateRecord:(NSIndexPath *)indexPath {
NSNumber *theId;
NSUInteger pos = [indexPath row];
id o;
if (self.results) {
theId = [self.results objectAtIndex:pos];
} else {
NSString *key = [[self.index objectAtIndex:[indexPath section]] objectAtIndex:0];
NSArray *data = [self.cache objectForKey:key];
theId = [data objectAtIndex:pos];
}
Db *db= [Db currentDb];
o = [db loadById:[self returnItemClass] theId:[theId intValue]];
return o;
}
最佳答案
- 预加载数据
- Do your own drawing
关于iphone - 如何解决 UITableView 滚动慢的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/695814/