每次滚动tableview时,单元格中的图像都会发生变化

时间:2015-11-02 04:57:14

标签: ios objective-c uitableview

在下面的代码中,每当我滚动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;
        }];
    }
}];

3 个答案:

答案 0 :(得分:2)

0000000000400990 <incby_tidgate>:
  400990:       31 c0                   xor    %eax,%eax
  400992:       f0 0f b1 3d 3a 1a 20    lock cmpxchg %edi,0x201a3a(%rip)        # 6023d4 <tidgate>
  400999:       00
  40099a:       0f 94 c0                sete   %al
  40099d:       eb 1a                   jmp    4009b9 <L01>
  40099f:       90                      nop
  4009a0:L00    64 48 8b 04 25 f8 ff    mov    %fs:0xfffffffffffffff8,%rax
  4009a7:       ff ff
  4009a9:       ff 40 04                incl   0x4(%rax)
  4009ac:       31 c0                   xor    %eax,%eax
  4009ae:       f0 0f b1 3d 1e 1a 20    lock cmpxchg %edi,0x201a1e(%rip)        # 6023d4 <tidgate>
  4009b5:       00
  4009b6:       0f 94 c0                sete   %al
  4009b9:L01    84 c0                   test   %al,%al
  4009bb:       74 e3                   je     4009a0 <L00>
  4009bd:       48 ff 05 e4 17 20 00    incq   0x2017e4(%rip)        # 6021a8 <ary+0xf8>
  4009c4:       48 ff 05 9d 19 20 00    incq   0x20199d(%rip)        # 602368 <ary+0x2b8>
  4009cb:       c7 05 ff 19 20 00 00    movl   $0x0,0x2019ff(%rip)        # 6023d4 <tidgate>
  4009d2:       00 00 00
  4009d5:       c3                      retq
  4009d6:       66 2e 0f 1f 84 00 00    nopw   %cs:0x0(%rax,%rax,1)
  4009dd:       00 00 00

在从网址加载图片之前添加图片的默认占位符,正在重复使用单元格,它会占用上一张图片

答案 1 :(得分: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];
      }
}];

答案 2 :(得分:0)

您可以使用Afnetworking类。

只需导入 #import“UIImageView + AFNetworking.h”

并使用此行: -

[cell.imgProfile setImageWithURL:imgurl placeholderImage:[UIImage imageNamed:@“”]];

Imgurl是您从回复

获取的图片网址