Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 下载缩略图图像后重新加载UItableViewCell_Ios_Objective C_Uitableview - Fatal编程技术网

Ios 下载缩略图图像后重新加载UItableViewCell

Ios 下载缩略图图像后重新加载UItableViewCell,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个应用程序,在tableViewCell中下载图像,然后将其设置为缩略图。我的问题是,我看到缩略图刷新的唯一方法是单击另一个选项卡,然后返回到tableViewCell所在的选项卡。下面是设置缩略图的代码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @

我有一个应用程序,在tableViewCell中下载图像,然后将其设置为缩略图。我的问题是,我看到缩略图刷新的唯一方法是单击另一个选项卡,然后返回到tableViewCell所在的选项卡。下面是设置缩略图的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    PFObject *message = [self.messages objectAtIndex:indexPath.row];
    cell.textLabel.text = [message objectForKey:@"username"];
    cell.imageView.image = [UIImage imageNamed:@"Treehouse.png"];
    PFFile *thumbnail = nil;

   if (thumbnailArray.count > 0){
    thumbnail = [[thumbnailArray objectAtIndex:indexPath.row] objectForKey:@"imageFile"];
   dispatch_async(backgroundQueue, ^{

           NSURL *thumbnailURL = [[NSURL alloc]initWithString:thumbnail.url];
           NSData *thumbnailData = [NSData dataWithContentsOfURL:thumbnailURL];

          [[cell imageView]setImage:[UIImage imageWithData:thumbnailData]];
       [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];


   });
   }


    return cell;
}

我建议您使用AFNetworkings类别UIImage+AFNetworking,它允许您只需执行[imageView setImageWithURL:(NSURL)],即可在后台管理所有内容。您不必担心加载图像、缓存图像或诸如此类的事情。它将为您缓存它

问题是

  • 单元格已加载
  • 图像下载已启动
  • 图像下载后,单元格将重新加载
  • 图像将再次下载
  • 没有缓存图像或加载图像的逻辑
  • 声明NSMutableDictionary
    @property(strong)NSMutableDictionary*imagesDictionary并实例化它
    self.imagesDictionary=[NSMutableDictionary]

    在索引路径行的单元格中,在下载缓存之前检查缓存是否存在

    PFFile *thumbnail = nil;
    
    if (thumbnailArray.count > 0)
    {
     //If image is present in cache load from cache
    UIImage *image = [self.imagesDictionary objectForKey:indexPath];
    if (image)
    {
        [cell.imageView setImage:image];
    }
    else
    {
        //If Image is not present fire a download
         thumbnail = [[thumbnailArray objectAtIndex:indexPath.row] objectForKey:@"imageFile"];
         dispatch_async(backgroundQueue, ^{
    
            NSURL *thumbnailURL = [[NSURL alloc]initWithString:thumbnail.url];
            NSData *thumbnailData = [NSData dataWithContentsOfURL:thumbnailURL];
    
             //Cahce it to your image dictionary
            [self.imagesDictionary setObject:[UIImage imageWithData:thumbnailData] forKey:indexPath];
             //Reload the dictionary
            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
    
    
        });
    
    
    }
    

    下面的代码帮助解决了我的问题

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
        PFObject *message = [self.messages objectAtIndex:indexPath.row];
        cell.textLabel.text = [message objectForKey:@"username"];
        cell.imageView.image = [UIImage imageNamed:@"Treehouse.png"];
        PFFile *thumbnail = nil;
    
       if (thumbnailArray.count > 0){
        thumbnail = [[thumbnailArray objectAtIndex:indexPath.row] objectForKey:@"imageFile"];
       dispatch_async(backgroundQueue, ^{
    
               NSURL *thumbnailURL = [[NSURL alloc]initWithString:thumbnail.url];
               NSData *thumbnailData = [NSData dataWithContentsOfURL:thumbnailURL];
               dispatch_sync(dispatch_get_main_queue(), ^{
                   [[cell imageView]setImage:[UIImage imageWithData:thumbnailData]];
                   [tableView reloadInputViews];
               });
       });
       }
    
    
        return cell;
    }
    

    问题是重新加载TableView是在一个单独的线程上调用的。一旦我将图像的设置移动到主线程并从那里调用ReloadInputView,我的问题就解决了

    [需要单元格布局]我已放置[单元格布局需要];在声明单元格变量之后,我的问题仍然存在。您建议我将该方法放在何处?您正在后台线程上更新UI。大禁忌。将其分派到主线程。我放入Dispatch_sync以获取主线程,然后调用[talbeView reloadInputViews];它起作用了。改用SDWebImage。