1

當loadImage的回調塊在下面運行時,表格單元可能已被重用。所以圖像被應用到「imageView」與這個重用單元格無關,它是舊單元格的圖像。UITableViewCell使用reuseidentifier給出不想要的結果與回調塊

如果我爲每個具有圖像的單元格設置唯一標識符,問題就會消失。但是這會給很多結果帶來糟糕的表現。

我能以某種方式在回調塊中使用相同的重用標識符並讓圖像在正確的單元格中出現嗎?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSDictionary *place; 
    PlaceTableViewCell *cell; // UITableViewCell subclass 
    NSString *identifier = @"PlaceTableViewCell"; 

    if (cell == nil) { 

     NSArray *objects; 

     objects = [[NSBundle mainBundle] loadNibNamed:@"PlaceTableViewCell" owner:self options:nil]; 

     for(id object in objects) { 

      if([object isKindOfClass:[PlaceTableViewCell class]]) {     
       cell = (PlaceTableViewCell *)object;     
       break; 
      } 
     } 
    } 

    UIImageView *imageView; 
    if((imageView = (UIImageView*)[cell viewWithTag:1])) { 

     NSString *filename; 
     int placeImageId = 0; 
     place = [places objectAtIndex:indexPath.row]; 

     if(place) { 

      placeImageId = [[d objectForKey:@"placeImageId"] intValue]; 

      if(placeImageId) { 

       [[RestAPIConnector sharedInstance] loadImage :placeImageId :@"thumb" :^(NSString *response){ 

        NSDictionary *image = [response JSONValue]; 

        if ([image objectForKey:@"img"]) { 

         NSString *b64Img = [image objectForKey:@"img"]; 
         UIImage *ui = [UIImage imageWithData:[Base64 decode:b64Img]]; 

         imageView.image = ui; 
        } 
       }];    
      } 
     } 
    } 

    return cell; 
} 

回答

1

這是我正在做的。的

而不是直接使用電池,我傳遞的索引路徑

if(user.profileImage == nil) 
{ 
    if (self.tableView.dragging == NO && self.tableView.decelerating == NO) { 
     NSLog(@"file for user %d doesn't exist", [user.userId intValue]); 
     [self startUserProfileImageDownload:user forIndexPath:indexPath]; 
    } 
} 
else 
{ 
    cell.profileImageView.image = user.profileImage; 

} 

一旦下載完成後,使用索引路徑檢索單元,並更新圖像

MessageCell *cell = (MessageCell *)[self.tableView cellForRowAtIndexPath:path]; 

// Display the newly loaded image 
cell.profileImageView.image = user.profileImage; 
CALayer *roundedLayer = [cell.profileImageView layer]; 

MessageCell是我的自定義單元格。如果您沒有使用客戶單元格,則可以使用標籤將imageView取回。

1

我想創建一個字典來保存圖像,然後試圖從字典中cellForRowAtIndexPath:

@property(retain)NSMutableDictionary *imageData; 

//... 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    //... 

    UIImageView *imageView; 
    if((imageView = (UIImageView*)[cell viewWithTag:1])) { 
     int placeImageId = 0; 
     place = [places objectAtIndex:indexPath.row]; 
     if(place) { 
      placeImageId = [[d objectForKey:@"placeImageId"] intValue]; 
      if(placeImageId) { 
       NSObject *image = [imageData objectForKey:[NSNumber numberWithInt:placeImageId]]; 
       if ([image isKindOfClass:[UIImage class]) { 
        imageView.image = (UIImage *)image; 
       } else if (![image isEqual:@"downloading"]) { 
        [imageData addObject:@"downloading" forKey:[NSNumber numberWithInt:placeImageId]]; 
        [[RestAPIConnector sharedInstance] loadImage:placeImageId onSuccess:^(NSString *response){ 
         NSDictionary *image = [response JSONValue]; 
         if ([image objectForKey:@"img"]) { 
          NSString *b64Img = [image objectForKey:@"img"]; 
          [imageData addObject:[UIImage imageWithData:[Base64 decode:b64Img]] forKey:[NSNumber numberWithInt:placeImageId]]; 
         } 
        }];    
       } 
      } 
     } 
    } 
    return cell; 
} 

一些潛在的優化閱讀:

  • 作爲@ Jun1st的樣品中,不在滾動時加載單元格的圖像
  • 將下載操作添加到NSOperationQueue,並優先考慮最近請求的操作(優先顯示已滾動的操作的優先級)
  • 將下載的圖像保存到文件系統並在那裏檢查首先
相關問題