iOS表视图性能低下

时间:2012-03-11 17:15:05

标签: ios uitableview

我真的陷入僵局。我有一张带有uilabels和图像的桌面视图。使用HJcache库下载图像。一切正常,但非常慢,这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *currentCellId = @"currentCell";
NSUInteger currentRow = [indexPath row];
HJManagedImageV* currentImage;
UILabel *textLabel;
UIImageView *onlineStatusView;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:currentCellId];

if (cell == nil) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:currentCellId];
    currentImage = [[HJManagedImageV alloc] initWithFrame:CGRectMake(2,2,40,40)];
    [currentImage setTag:999];
    [cell addSubview:currentImage];
    textLabel = [[UILabel alloc] initWithFrame:CGRectMake(50.0f, 2.0f, cell.frame.size.width - 50.0f, cell.frame.size.height-4.0f)];
    [textLabel setTag:777];
    [textLabel setFont:[UIFont boldSystemFontOfSize:12.0f]];
    [cell addSubview:textLabel];
    if ([[[dataArray objectAtIndex:currentRow] objectForKey:@"online"] integerValue] == 1){
        onlineStatusView = [[UIImageView alloc] initWithFrame:CGRectMake(cell.frame.size.width-15.0f,
                                                                         cell.frame.size.height/2,
                                                                         10.0f,
                                                                         10.0f)];
        [onlineStatusView setTag:888];
        [cell addSubview:onlineStatusView];
    }
} else{
    onlineStatusView = (UIImageView *)[cell viewWithTag:888];
    textLabel = (UILabel *)[cell viewWithTag:777];
    currentImage = (HJManagedImageV*)[cell viewWithTag:999];
    [currentImage clear];
}

currentImage.url = [NSURL URLWithString:[[dataArray objectAtIndex:currentRow] objectForKey:@"image"]];
[cache manage:currentImage];
[currentImage setOpaque:YES];
currentImage = nil;

NSString *nameString = [NSString stringWithFormat:@"%@ %@",
                  [[dataArray objectAtIndex:currentRow] objectForKey:@"firstName"],
                  [[dataArray objectAtIndex:currentRow] objectForKey:@"lastName"]];
[textLabel setText:nameString];
[textLabel setOpaque:YES];
textLabel = nil;

if (onlineStatusView){
    [onlineStatusView setImage:[UIImage imageNamed:@"Online@2x.png"]];
    [onlineStatusView setOpaque:YES];
    onlineStatusView = nil;
}

return cell;

}

一切看起来都不错,我删除了所有不必要的分配,缓存的图像,但都无济于事。我做错了什么?

ps现在我已经有大约200行,但是当它是20时就会出现错误。我想我的错误确实很愚蠢。

2 个答案:

答案 0 :(得分:1)

这里有几个问题:

  • 你在将currentImage存在之前将其设置为不透明,所以这没有任何效果。在创建并将图像作为子视图添加到单元格后执行此操作
  • 你一直在创造新的标签。这些只应添加一次,就像你的currentImage一样,只需要修改文本
  • 同样适用于在线状态视图。在制作单元格时创建一次,然后根据您的模型将其隐藏或不隐藏。

我不知道您正在使用的库,所以无法对此发表评论。

答案 1 :(得分:0)

在缓存周围插入一些NSLog():管理调用以查看它们的持续时间。不要担心你的200行,只为可见行调用cellForRowAtIndexPath。