iphone - 如何解决 UITableView 滚动慢的问题

标签 iphone objective-c performance

我是第一次在真实设备上进行测试,在解决了一些明显的性能问题后,我一直在研究如何实现平滑滚动。

这是我的做法:

  • 数据在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;
}

最佳答案

  1. 预加载数据
  2. Do your own drawing

关于iphone - 如何解决 UITableView 滚动慢的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/695814/

相关文章:

iphone - 单个请求到多个异步响应

objective-c - 将回调/闭包从 Objective C 转换为 Swift

performance - 为什么让 Haskell 变得懒惰会影响性能?

iphone - UITableView onTouch 隐藏键盘

iphone - 让 UIImageView 填满整个 UITableViewCell

iphone - 是否可以将弹出框内的导航栏高度更改为默认高度?

c# - 我可以通过 Linq to Sql 中的 InserAllOnSubmit() 插入的最大记录数是多少

objective-c - 在大型 NSString 中有效地找到许多关键字中的第一个

iphone - 如何为iPhone游戏制作暂停菜单?

iphone - 如何在 UIImageView 中为图像应用宽高比后知道图像大小