UITableView使用UIImage慢慢滚动

时间:2012-10-30 21:50:19

标签: objective-c ios xcode uitableview

当我将图像添加到UITableView时,我注意到第一次将它们添加到单元格时,我得到一个不稳定的滚动。

我没有远程加载它们,而是我的代码看起来像这样:

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

MyObject *object = [self.myObjects objectAtIndex:indexPath.row]; 
cell.imageView.image = [UIImage imageNamed:object.imageName];

return cell;

(没有if(cell == nil)行作为im注册视图中的单元格类加载)

这是一种不好的方式吗?如果是这样,我应该如何将主包中的图像添加到我的单元格的imageView?

1 个答案:

答案 0 :(得分:1)

  1. 尝试缩小图片尺寸

  2. 如果因任何原因无法缩小尺寸,请尝试以下代码(operationQueue是NSOperationQueue实例的ivar)

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
        [operationQueue addOperationWithBlock:^{
            NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"name" ofType:@"jpg"];
            UIImage *image = [[UIImage alloc] initWithContentsOfFile:imagePath];
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
                cell.imageView.image = image;
            }];
        }];
        return cell;
    }