首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS表视图性能低下

iOS表视图性能低下
EN

Stack Overflow用户
提问于 2012-03-12 01:15:05
回答 2查看 323关注 0票数 0

我真的陷入僵局了。我有一个有and标签和图片的桌面视图。使用HJcache库下载图像。一切都很好,但是非常非常慢,下面是我的代码:

代码语言:javascript
复制
- (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;

}

一切看起来都很好,我删除了所有不必要的分配,缓存的图像,但都没有用。我哪里做错了?

现在我已经有大约200行了,但是bug在20行的时候就开始了,我想我的错误真的很愚蠢。

EN

回答 2

Stack Overflow用户

发布于 2012-03-12 01:34:31

这里有几个问题:

  • 您在currentImage存在之前将其设置为不透明,因此这没有任何效果。在创建图像并将其作为子视图
  • 添加到单元格后执行此操作。您一直在创建新标签。这些应该只添加一次,就像您的currentImage一样,只需将文本modified
  • 添加到online status视图即可。在创建单元格时创建一次,并根据您的模型将其隐藏或不隐藏。

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

票数 1
EN

Stack Overflow用户

发布于 2012-03-12 02:11:16

在缓存中插入一些NSLog():管理调用以查看它们真正持续了多长时间。不要担心您的200行,cellForRowAtIndexPath只对可见的行调用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9657039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档