【发布时间】:2012-05-11 03:04:34
【问题描述】:
我有一个由 UIImageView 和 UILabel 组成的自定义 UITableViewCell。单元格为 320x104px,imageView 占据了整个区域,标签位于前面。只有 8 个单元格。
在 ViewDidLoad 中,我预先创建了所有需要的图像,并将它们以正确的尺寸缓存在字典中。
当我滚动 UITableView 时,每次遇到新单元格时都会出现明显的延迟。这对我来说毫无意义,因为它使用的图像已经创建和缓存。我对单元格的要求只是让它的 UIImageView 呈现图像。
我正在使用一个自定义单元格,其视图位于 xib 中,并配置我的 UITableView 以将其用于:
[self.tableView registerNib:[UINib nibWithNibName:@"ActsCell" bundle:nil] forCellReuseIdentifier:myIdentifier];
单元创建和配置:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* reuseIdentifier = @"ActsCell";
ActsCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
// Configure the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)configureCell:(ActsCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
Act* act = [self.acts objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.title.text = act.name;
cell.imageView.image = [self.imageCache objectForKey:act.uid];
}
什么可能导致滞后?尝试做任何异步操作似乎没有任何好处,因为所有耗时的工作都已完成。
【问题讨论】:
-
我能想到两件事。一是出队失败是因为您没有正确设置
reusableIdentifier。另一个是图像的大小。即使加载了图像,CG 也需要在显示之前对图像数据进行处理,这可能涉及到缩放、合成等成本高昂的操作。 -
@Lvsti 你能详细说明我没有正确设置reuseIdentifier吗?
-
你说得对,可能不是这样,否则出队时你不会得到任何单元格......
-
如果你的意思是我不使用 if(cell == nil){ //Create cell },如果使用 registerNib:forCellReuseIdentifier: 则不需要这样做,因为单元格创建被推迟到 VC。
-
是的,我意识到了这一点。至于图片,它们的尺寸是多少?
标签: performance uitableview uiimageview uiimage