0

我正在像这样构建我的 cellViews:

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString* cellIdentifier=@"cell";

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
    }

    UIImageView cellView = [[UIImageView alloc] initWithFrame:rectCellFrame];

    NSError* error=nil;
    NSData* imageData = [NSData dataWithContentsOfURL:imageArray[indexPath.row] options:NSDataReadingUncached error:&error];

    UIImage* theImage= [UIImage ImageWithData:imageData];

    [cellView setImage:theImage];

    [cell addSubView:cellView];

    .
    .
    .
    .

    [cell addSubView:moreViews];

}

由于加载时间(即使图像被缓存)非常慢,我需要让它并发。但我仍想将我的代码与 UIViews/UIImageViews 一起使用。有没有办法让我显示一个占位符,当相关时,即 cellView 从所有子视图完成构建,更新图像而不是占位符?

4

1 回答 1

1

当然。您可以在异步任务中设置所有繁重的慢代码。当需要下载图像时,它通常会关闭。我确信至少有 1 个关于 Table Views 的 WWDC 视频涵盖了它,但我不知道它现在是哪一个或有多旧。

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // place holder image for the moment
    [cellView setImage:placeHolderImage];

    // run code to get the real image in asynchronous task 
    dispatch_async(self.contextQueue, ^{
        UIImage *realImage = [thingy imageFromTimeConsumingTask];
        // update cell on main thread (you need to do all UI stuff on main thread)
        dispatch_async(dispatch_get_main_queue(), ^{
            [cellView setImage:realImage];
        });
    });
}
于 2013-03-12T19:23:23.027 回答