【问题标题】:Async image loading from url inside a UITableView cell - wrong image while scrolling从 UITableView 单元格内的 url 加载异步图像-滚动时图像错误
【发布时间】:2014-03-11 14:07:17
【问题描述】:

我有一个 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];           
        }
}` 

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

【问题讨论】:

  • 听起来问题在于重用 tableview 中的单元格,您能否将代码粘贴到您创建并返回 tableview 单元格的位置。
  • @Preson 你能检查我编辑的问题吗...
  • 请在您的代码中添加- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 部分。 willDisplayCell 在屏幕上渲染单元格之前调用,内容在cellForRowAtIndexPath 方法中设置。
  • @Preson 那是第一个..

标签: ios iphone objective-c ipad uitableview


【解决方案1】:

这很可能是由于细胞的重复使用。可能发生的事情是您触发了下载图像的异步请求,而在发生这种情况时,您滚动表格并重复使用该单元格,因此当图像完成下载时,该新单元格获取图像而不是最初请求的图像。

我认为您将不得不找到另一种方法来执行此操作,或者将该请求从 prepareForReuse 上的单元格中分离出来

很多人都在针对这种情况推荐这个类“SDWebImage”(虽然我还没有测试过)

【讨论】:

    【解决方案2】:

    过去 2 年我一直在使用 SDWebImage,并在 5 个应用程序中使用过,真的很简单,我会说这是在后台加载图像并缓存的最佳方式。

    请试一试。你会喜欢的。 一个不错的教程是here

    【讨论】:

      猜你喜欢
      • 2013-05-15
      • 1970-01-01
      • 1970-01-01
      • 2016-06-27
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多