如何解决UITableView中的慢速滚动问题

时间:2009-03-30 02:06:40

标签: iphone objective-c performance

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

这就是我的所作所为:

  • 数据是sqlite
  • 我有一个带标题的小数组
  • 我在每个标题数组中都有来自Db的Id列表

e.g。

标题A.    Ids = 1,2; 标题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 个答案:

答案 0 :(得分:5)

  1. 预加载数据
  2. Do your own drawing
相关问题