2015-11-02 41 views
-1

在下面的代碼中,無論何時滾動tableview,每個單元格中的圖像都在變化,這不應該發生。請幫忙。提前致謝。當tableview被滾動時,單元格中的圖像每次都在變化

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
UserDetails *userDetails = [arrUserDetails objectAtIndex:indexPath.row]; 
static NSString *CellIdentifier = @"CustomCell"; 
__weak TableViewCell *cell = (TableViewCell *)[_tableViewUsername dequeueReusableCellWithIdentifier:CellIdentifier]; 
cell.tag = indexPath.row; 
cell.userName.text = userDetails.userName; 
    [self.operationQueue addOperationWithBlock:^{ 
    NSURL *aURL = [NSURL URLWithString:userDetails.userImageURL]; 
    NSError *error = nil; 
    NSData *data = [NSData dataWithContentsOfURL:aURL options:nil error:&error]; 
    UIImage *image = nil; 
    if (cell.tag == indexPath.row) 
    { 
     image = [UIImage imageWithData:data]; 
     [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
      cell.customImageView.image = image; 
      cell.customImageView.contentMode = UIViewContentModeScaleToFill; 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     }]; 
    } 
}]; 
+0

瞭解更多有關如何'UITableView'工作伴侶的圖像的URL。細胞一直出列並重復使用。這就是爲什麼你看到圖像去你認爲是錯誤的單元格,但對於iOS,他們是正確的,因爲'cell.tag == indexPath.row'。您需要比標籤更多的可靠參考。 – NSNoob

+0

使用自定義單元格並定義它的屬性,如名稱標籤和圖像視圖。這將保持堅實的參考,哪個單元是哪個單元並解決由當前代碼引起的問題,即每次調用委託方法時創建UIElements。 – NSNoob

+0

@muku是正確的 - 您不會設置默認圖像,或者至少在加載新圖像時清除舊圖像。另外考慮使用像SDWebImage提供緩存以獲得更好的性能 – Paulw11

回答

2
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
UserDetails *userDetails = [arrUserDetails objectAtIndex:indexPath.row]; 
static NSString *CellIdentifier = @"CustomCell"; 
__weak TableViewCell *cell = (TableViewCell *)[_tableViewUsername dequeueReusableCellWithIdentifier:CellIdentifier]; 
cell.tag = indexPath.row; 
cell.userName.text = userDetails.userName; 

//Add Default placeholder 
cell.customImageView.image = [UIImage imageNamed:@"Default.png"]; 

    [self.operationQueue addOperationWithBlock:^{ 
    NSURL *aURL = [NSURL URLWithString:userDetails.userImageURL]; 
    NSError *error = nil; 
    NSData *data = [NSData dataWithContentsOfURL:aURL options:nil error:&error]; 
    UIImage *image = nil; 
    if (cell.tag == indexPath.row) 
    { 
     image = [UIImage imageWithData:data]; 
     [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
      cell.customImageView.image = image; 
      cell.customImageView.contentMode = UIViewContentModeScaleToFill; 
      cell.accessoryType = UITableViewCellAccessoryCheckmark; 
     }]; 
    } 
}]; 

添加默認佔位符圖像從URL加載它,當細胞被重用把前面的圖像之前

0

您可以使用SDWebImage.framework加載圖像

[cell.customImageView sd_setImageWithURL:[NSURL URLWithString:userDetails.userImageURL] placeholderImage:nil options:SDWebImageCacheMemoryOnly completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { 
     if (image) { 
      [cell.customImageView setImage:image]; 
     } 
}]; 
0

您可以使用Afnetworking類。

簡單地導入#進口 「的UIImageView + AFNetworking.h」

,並使用此行: -

[cell.imgProfile setImageWithURL:imgUrl的placeholderImage:[UIImage的imageNamed:@ 「」]] ;

imgUrl的是你從響應得到

相關問題