2013-04-16 82 views
0

我正在使用GCD將圖像從網絡載入我的UITableViewCell。但是,錯誤的圖像正在顯現。錯誤的圖像顯示在UITableviewcell中

這裏是代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
    UITableViewCell *cell = [self.newsTable dequeueReusableCellWithIdentifier:@"newsTableCell"]; 
    NSString *user = [usernames objectAtIndex:[indexPath row]]; 
    NSString *stat = [statistics objectAtIndex:[indexPath row]]; 
    NSString *imagePath = [appDelegate.URL stringByAppendingString:@"ImageName"]; 
    UIImageView *userImageView = (UIImageView *)[self.view viewWithTag:80]; 


     NSString *imagePath1 = [imagePath stringByAppendingString:[imagepaths objectAtIndex:[indexPath row]]]; 

     NSURL *url = [NSURL URLWithString:imagePath1]; 
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); 
    dispatch_async(queue, ^{ 
     NSData *data = [NSData dataWithContentsOfURL:url]; 
     UIImage *img = [[UIImage alloc] initWithData:data]; 
     userImageView.layer.cornerRadius = 5.0; 
     userImageView.layer.masksToBounds = YES; 
     dispatch_sync(dispatch_get_main_queue(), ^{ 
     [userImageView setImage:img]; 
     }); 
    }); 
return cell 
} 

請讓我知道,我要去的地方錯了。

+0

拉穆Pasupuleti請你詳細說明究竟是什麼解決了你的問題?因爲我擁有完全相同的問題 – Jonathan

+0

@Jonathan,我正在使用標籤加載圖像。所以,我用自定義的UITableViewCell類替換了它。 –

+0

哦好吧,但不會細胞也解決了這個問題? – Jonathan

回答

1

您並未全部使用您的cell變量,您正在設置來自self.viewUIImageView圖像。圖像永遠不會在表格的單元格上正確設置。

+0

感謝您的回答。現在圖像顯示正確,但圖像只在滾動時加載。 –

+1

這是推薦的行爲,移動設備受限於帶寬和RAM,因此只會因爲顯示錶頭而下載數百張圖片,這會損害用戶。然而,如果你絕對需要這樣做,我會建議從單元格創建方法(可能在viewDidAppear)中檢索圖像,然後只有當它們完成加載或滾動表格並且新單元格爲爲已下載的圖像出列。 –

+0

感謝您的信息 –