简体   繁体   English

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

[英]Images in cell are changing everytime when tableview is scrolled

In the below code whenever I am scrolling the tableview, images in each cell are changing, which shouldn't happen. 在下面的代码中,每当我滚动表格视图时,每个单元格中的图像都会发生变化,这是不应该发生的。 Please help. 请帮忙。 Thanks in advance. 提前致谢。

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

Add Default placeholder for image before loading it from url,As cells are being reused it takes the previous image 从网址加载图片之前,为图片添加默认占位符,因为单元格被重用,它会获取前一张图片

You can use SDWebImage.framework to load image 您可以使用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];
      }
}];

You can use Afnetworking class for this . 您可以为此使用Afnetworking类。

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

And use this line:- 并使用此行:

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

Imgurl is the image Url which you are getting from response Imgurl是您从响应中获得的图片网址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM