2012-10-09 82 views
1

我有以下cellForRowAtIndexPath方法的代码:的UITableViewController滚动滞后与大量图像

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"champion cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    UIImage *cellIcon = [UIImage imageNamed:[arrayOfChampionPictures objectAtIndex:indexPath.row]]; 
    [[cell imageView] setImage:cellIcon]; 

    cell.imageView.frame = CGRectMake(0.0f, 0.0f, 70.0f, 70.0f); 

    cell.imageView.layer.cornerRadius = 7; 
    [cell.imageView.layer setMasksToBounds:YES]; 

    cell.textLabel.text = [arrayOfChampionNames objectAtIndex:indexPath.row]; 

    return cell; 
} 

arrayOfChampionNames存储在NSMutableArray包含图片名称本地数据库。这是关于我的UITableView中的图像103单元格。它在第一次滚动时从头到尾滞后,然后顺利滚动。模拟器上没有滞后。


可能的解决方案,我想出了,但我不知道如何实现它们滚动后

  • 加载图像,
  • 预加载图像数据到UITableView的。
+0

您需要重用您的单元格并将图像大小调整为单元格图像视图的大小 - 这可能是您遇到滞后现象的原因。 – sooper

+0

@sooper谢谢你的回应,我会尝试! – ignotusverum

回答

0

你忘了的东西:

static NSString *CellIdentifier = @"champion cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
} 

这样你实际上会利用您所创建的可重复使用的标识符。

+0

感谢您的回应,但它仍然laggy – ignotusverum

+0

,我忘了提及,在模拟器上没有滞后 – ignotusverum

+0

@anonymous有道理,永远不要相信模拟器与这种事情。你的电脑有更多的RAM,然后你打电话。你使用的图像有多大? –

2

我会告诉你滞后来自哪里 - 拐角半径。预先计算圆角的图像。动态地执行它们会导致滚动性能下降。

+0

您先生是天才! –