在UITableView单元格中从url异步加载图像-滚动时图像错误



我有一个IPhone应用程序,其中我从数组异步加载图像,在cellforrowatindexpath中使用这个,

 `   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    sCell *cell = (sCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[sCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
     NSMutableDictionary *dicttable=[sarray objectAtIndex:indexPath.section];
            NSString *icon= [dicttable objectForKey:@"thumb_image"];
            cell.image.image = [UIImage imageNamed:@"thumbnail-default.png"];
            if(icon.length!=0)
            {
                if(![[ImageCache sharedImageCache] hasImageWithKey:icon])
                {   cell.image.image = [UIImage imageNamed:@"thumbnail-default.png"];
                    NSArray *myArray = [NSArray arrayWithObjects:cell.image,icon,@"thumbnail-default.png",[NSNumber numberWithBool:NO],nil];
                    AppDelegate *appDelegate = (AppDelegate  *)[[UIApplication sharedApplication] delegate];
                    [appDelegate performSelectorInBackground:@selector(updateImageViewInBackground:) withObject:myArray];
                }
                else
                {
                    cell.image.image = [[ImageCache sharedImageCache] getImagefromCacheOrUrl:icon];

                }
            }`

和所有工作正常,因为我启用了分页,我需要调用请求来加载下一组值,像这样

`- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section==[sarray count]-1)
    {

             [self performSelectorInBackground:@selector(loadingfeedcntents:) withObject:count];           
        }
}` 

但这里的问题是,当我滚动表视图错误的图像被分配到自定义单元格中的图像视图,有人能帮助我吗?

这很可能是由于单元格的重用。可能正在发生的事情是,你触发了一个异步请求下载图像,而这正在发生,你滚动表格和单元格被重用,所以当图像下载完成,新的单元格得到的图像,而不是原来的请求它。

我认为你必须找到另一种方法来做到这一点或从prepareForReuse上的单元格中分离请求

对于这种情况,许多人推荐使用"SDWebImage"类(尽管我还没有测试过)

我已经使用SDWebImage两年了,已经在5个应用程序中使用过,非常简单,我想说这是在后台加载缓存图像的最佳方式。

请试一下。你会喜欢的。

最新更新