iOS表视图低性能

iOS表视图低性能,ios,uitableview,Ios,Uitableview,我真的陷入了僵局。我有一个带有UILabel和图像的tableview。使用HJcache库下载图像。所有操作都很好,但速度非常慢,下面是我的代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *currentCellId = @"currentCell"; NSUInteger currentRow

我真的陷入了僵局。我有一个带有UILabel和图像的tableview。使用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行,但是bug从20行开始。我想我的错误真的很愚蠢。

这里有几个问题:

  • 在currentImage存在之前,您将其设置为不透明,因此这不起作用。在创建图像并将其作为子视图添加到单元格后执行此操作
  • 您一直在创建新标签。这些应该只添加一次,就像您当前的图像一样,只需修改文本即可
  • 在线状态视图也是如此。创建单元时创建一次,并根据模型将其隐藏或不隐藏

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

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

谢谢。我做了一些更改,但在设备上我再次收到内存警告。请检查我的新代码。我以为你的问题是滚动速度慢,而不是内存?如果是内存,这可能与HJCache库有关,我不知道您是否正确使用了它,因为我从未使用过它。好的,我删除了所有图像,但所有图像都保持不变。只有标签的Tableview有一个滚动慢的问题。正如我在下面评论的,这个函数初始化调用1900次。你是在任何地方手动调用它吗?你的numberOfSections/numberOfRows方法中有什么?是的,我知道可见单元格。我刚刚检查过启动时这个函数调用1900次!我觉得不太好,不是吗?最好在这里回答: