0
  • 我正在为一个学校项目制作一个简单的 reddit 应用程序。我正在通过带有库的 json(例如http://www.reddit.com/.json )从 reddit 加载我的数据。AFNetworking

  • 我用一个原型单元格显示每个 reddit 线程,其中包含一个UIImageView用于帖子缩略图的单元格。

  • 我正在尝试使用AFNetworking该方法延迟加载图像 setImageWithURLRequest

问题:当应用程序启动所有缩略图时,当我向下滚动 tableView 时,它们会延迟加载。一旦单元格离开视图并且我向上滚动到它,缩略图就会被占位符图像替换——即使它在滚动过去之前加载了正确的缩略图。

方法中的相关代码cellForRowAtIndexPath。在setImageWithURLRequest块 中调用延迟加载

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"threadCell";
    SubredditThreadCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSDictionary *tempDictionary   = [self.subredditArrayFromAFNetworking objectAtIndex:indexPath.row];
    NSDictionary *singleThreadDict = [tempDictionary objectForKey:@"data"];

    if ( [[singleThreadDict objectForKey:@"thumbnail"] isEqualToString:@"nsfw"] ){
        cell.postImageThumbnail.image = [UIImage imageNamed:@"NSFWIcon"];
    }
    else if ([[singleThreadDict objectForKey:@"thumbnail"] isEqualToString:@"self"]){
        cell.postImageThumbnail.image = [UIImage imageNamed:@"selfIcon"];
    }
    else if ([[singleThreadDict objectForKey:@"thumbnail"] length] == 0){
        cell.postImageThumbnail.image = [UIImage imageNamed:@"genericIcon"];
    }
    else{
        NSURL   *thumbURL = [NSURL URLWithString:[singleThreadDict objectForKey:@"thumbnail"] ];


        [cell.postImageThumbnail setImageWithURLRequest:[NSURLRequest requestWithURL:thumbURL]
                                 placeholderImage:nil
                                 success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
                                    {
                                        if (request) {
                                            [UIView transitionWithView:cell.postImageThumbnail
                                                    duration:1.0f
                                                    options:UIViewAnimationOptionTransitionCrossDissolve
                                                    animations:^{
                                                        [cell.postImageThumbnail setImage:image];
                                                    }
                                                    completion:NULL];
                                        }
                                    }
                                    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
                                    {
                                        NSLog(@"failure loading thumbnail");
                                    }
         ];
    }

    return cell;
}
4

1 回答 1

1

图像下载完成后,不要直接更新单元格的 postImageThumbnail。

相反,告诉 UITableView 只刷新那个单元格,使用– reloadRowsAtIndexPaths:withRowAnimation:.

为了获得更好的性能,UITableView 将在单元格对象滚动到屏幕外后重新使用它,以显示当前可见的不同数据。(这就是为什么dequeueReusableCellWithIdentifier:以“dequeueReusable”而不是“makeNew”开头的原因。)这让 UITableView 只创建与可见的一样多的单元格,而不必创建和销毁与表中的行一样多的单元格。当您的网络请求成功时,cellsuccess:捕获的 将用于显示另一行,并且您正在覆盖行的图像。

于 2013-05-16T03:03:02.460 回答