2014-06-11 52 views
2

我有一個應用程序,其中tableViewCell下載圖像並設置爲縮略圖。我的問題是,我可以看到縮略圖圖像刷新的唯一方法是如果我點擊另一個選項卡,然後回到tabViewCell被保留的選項卡。以下是設置縮略圖的代碼。在縮略圖下載後重新加載UItableViewCell

- (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; 
} 
+0

'[cell layoutIfNeeded];' – Jano

+0

我已經放置了[cell layoutIfNeeded];在聲明單元格變量之後,我的問題依然存在。你在哪裏建議我放置該方法? – TheM00s3

+2

您正在後臺線程上更新您的用戶界面。大不,不。發送到主線程。 – Stonz2

回答

2

的問題是,

  1. 小區被加載。
  2. 圖像下載被觸發。
  3. 下載圖像後,單元格將重新加載。
  4. 圖像再次下載。
  5. 沒有圖像或邏輯的高速緩存加載圖像

聲明一個的NSMutableDictionary @property (strong) NSMutableDictionary *imagesDictionary;並創建實例self.imagesDictionary = [NSMutableDictionary dictionary];

在在索引路徑小區行,檢查高速緩衝存儲器的存在下載前它。

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]; 


    }); 


} 
1

我建議你使用AFNetworkings類的UIImage + AFNetworking它允許你只是做[ImageView的setImageWithURL:(NSURL)],其管理後臺的一切。您不必擔心加載圖像,緩存圖像或類似的東西。它會爲你緩存。

https://github.com/AFNetworking/AFNetworking

1

下面的代碼幫助解決我的問題

- (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是被稱爲一個單獨的線程。一旦我將圖像的設置移動到主線程並從那裏調用reloadInputViews,我的問題就解決了。