2017-05-14 38 views
-2

你好,大家好我从我的服务器下载每个产品的图片并进行设置,我的UITableView的自定义单元格内,向下滚动的UITableView后缓存他们在磁盘映像放置不当图像是错过放置我的意思是新细胞在搜索这个主题后采用旧的细胞的旧值我发现不同的解决方案要么使用prepareForReuse要么检查单元如果它的零或不是他们都不适合我案件。上滚动的实现代码如下目标C

CODE: MyCustomCell.h

#import <UIKit/UIKit.h> 
#import "mirsaProduct.h" 
#import "SVProgressHUD.h" 
#import "AFNetworking.h" 
#import "UIWebView+AFNetworking.h" 
#import "SDWebImageCompat.h" 
#import "SDWebImageDownloaderOperation.h" 
#import "SDWebImageDownloader.h" 
#import "SDImageCache.h" 
#import "SDWebImageDownloaderOperation.h" 
#import "SDWebImageManager.h" 
#import "SDWebImageDecoder.h" 
#import "SDWebImagePrefetcher.h" 

    @interface mirsaProductTableViewCell : UITableViewCell 
    @property (weak, nonatomic) IBOutlet UILabel *mirsaProductTitle; 
    @property (weak, nonatomic) IBOutlet UITextView *mirsaProductDescription; 
    @property (weak, nonatomic) IBOutlet UIActivityIndicatorView *mirsaProductActivityIndicatorView; 
    @property (weak, nonatomic) IBOutlet UIImageView *mirsaProductLogoImageView;  
    @end 


**MyCustomCell.m** 

#import "mirsaProductTableViewCell.h" 

@implementation mirsaProductTableViewCell 

@synthesize mirsaProductTitle = _mirsaProductTitle; 
@synthesize mirsaProductDescription = _mirsaProductDescription; 
@synthesize mirsaProductLogoImageView = _mirsaProductLogoImageView; 
@synthesize mirsaProductActivityIndicatorView = _mirsaProductActivityIndicatorView; 

-(void)prepareForReuse 
{ 

} 
- (void)awakeFromNib { 
    [super awakeFromNib]; 
    // Initialization code 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 





@end 

myTableViewController.h

#import <UIKit/UIKit.h> 
#import "mirsaCategories.h" 
#import "sharedManagers.h" 
#import "mirsaProductTableViewCell.h" 
#import "constant.h" 
#import "SVProgressHUD.h" 
#import "AFNetworking.h" 
#import "UIWebView+AFNetworking.h" 
#import "SDWebImageCompat.h" 
#import "SDWebImageDownloaderOperation.h" 
#import "SDWebImageDownloader.h" 
#import "SDImageCache.h" 
#import "SDWebImageDownloaderOperation.h" 
#import "SDWebImageManager.h" 
#import "SDWebImageDecoder.h" 
#import "SDWebImagePrefetcher.h" 
#import "mirsaProduct.h" 
#import "mirsaDetailedProductViewController.h" 
#import "mirsaProductDetailsTableViewController.h" 
@interface mirsaProductsTableViewController : UITableViewController 
@property (nonatomic,strong) mirsaCategories *currentCategory; 
@property (nonatomic,strong) NSMutableArray *listOfProducts; 
@property (nonatomic,strong) mirsaProduct *currentProduct; 

@end 

myTableViewController.m 由于没有使问题太长,我只是进口cellOfRowAtIndexPath以及相关的方法。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"mirsaProductCell"; 
    mirsaProductTableViewCell *cell = (mirsaProductTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 



    if (cell == nil) { 

     cell = [[mirsaProductTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] ; 
     cell.mirsaProductLogoImageView.layer.cornerRadius = 50.0f; 
     cell.mirsaProductLogoImageView.layer.borderWidth = 1.5f; 
     cell.mirsaProductLogoImageView.clipsToBounds = YES; 
     cell.mirsaProductLogoImageView.layer.borderColor = [UIColor colorWithRed:redRedColor green:redGreenColor blue:redBlueColor alpha:ALFA].CGColor; 
     mirsaProduct *currentProduct = [self.listOfProducts objectAtIndex:indexPath.row]; 

     cell.mirsaProductTitle.text = [NSString stringWithFormat:@"Product Code:%@",currentProduct.mirsaProductCode]; 
     cell.mirsaProductDescription.text = [NSString stringWithFormat:@"Description: %@",currentProduct.mirsaProductDescription]; 
     [self downloadProductLogoImageView:cell :indexPath.row]; 
     for (int i = 0; i < currentProduct.mirsaProductUrlImage.count; i++) { 
      [self downloadImageWithCached:currentProduct.mirsaProductUrlImage[i] :cell :indexPath.row :(NSInteger)i]; 
     } 
    } 


    else 

    { 
     cell.mirsaProductLogoImageView.layer.cornerRadius = 50.0f; 
     cell.mirsaProductLogoImageView.layer.borderWidth = 1.5f; 
     cell.mirsaProductLogoImageView.clipsToBounds = YES; 
     cell.mirsaProductLogoImageView.layer.borderColor = [UIColor colorWithRed:redRedColor green:redGreenColor blue:redBlueColor alpha:ALFA].CGColor; 
     mirsaProduct *currentProduct = [self.listOfProducts objectAtIndex:indexPath.row]; 

     cell.mirsaProductTitle.text = [NSString stringWithFormat:@"Product Code:%@",currentProduct.mirsaProductCode]; 
     cell.mirsaProductDescription.text = [NSString stringWithFormat:@"Description: %@",currentProduct.mirsaProductDescription]; 
     [self downloadProductLogoImageView:cell :indexPath.row]; 
     for (int i = 0; i < currentProduct.mirsaProductUrlImage.count; i++) { 
      [self downloadImageWithCached:currentProduct.mirsaProductUrlImage[i] :cell :indexPath.row :(NSInteger)i]; 
     } 
    } 


    return cell; 
} 






-(void)downloadProductLogoImageView:(mirsaProductTableViewCell *)cell :(NSInteger)index; 
{ 
    @autoreleasepool { 
     [cell.mirsaProductActivityIndicatorView startAnimating]; 
     mirsaProduct *currentProduct = [self.listOfProducts objectAtIndex:index]; 
     NSString *cachedKey = currentProduct.mirsaProductImage; 
     SDImageCache *imageCache = [[SDImageCache alloc] initWithNamespace:@"productLogoImageView"]; 
     [imageCache queryDiskCacheForKey:cachedKey done:^(UIImage *image, SDImageCacheType cacheType) 

     { 
      if (image) { 


       dispatch_async(dispatch_get_main_queue(), ^{ 
        [cell.mirsaProductActivityIndicatorView stopAnimating]; 
        cell.mirsaProductLogoImageView.image = image; 

       }); 
      }else{ 

       dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
        NSURL *url = [NSURL URLWithString:currentProduct.mirsaProductImage]; 
        [[SDWebImageDownloader sharedDownloader]downloadImageWithURL:url options:SDWebImageDownloaderLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) { 
        } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) { 




         if (finished && image){ 
          dispatch_async(dispatch_get_main_queue(), ^{ 

           [imageCache setMaxCacheAge:60*60*24]; 

           [imageCache storeImage:image 
            recalculateFromImage:NO 
               imageData:data 
               forKey:currentProduct.mirsaProductImage 
               toDisk:YES]; 
          }); 



          [cell.mirsaProductActivityIndicatorView stopAnimating]; 
          cell.mirsaProductLogoImageView.image = image; 


         } 


        }]; 









       }); 
      } 
     }]; 






    } 


} 

一些链接,我的堆栈溢出Tableview images chaging when scrolling using custom tableview发现,Incorrect cell data display when scrolling UITableView

回答

1

嗨,据我的经历,你的代码有没有需要做的为您解决问题的两大转变

  1. if(cell == nil){

    cell = [[mirsaProductTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] ; 
    cell.mirsaProductLogoImageView.layer.cornerRadius = 50.0f; 
    
在这个代码行

不为零,而不是使用小区标识使用

  • 下载图像,你不需要写那么多的代码只写

    [imageView sd_setImageWithURL:[NSURL URLWithString:@“http://www.example.com/path/to/image.jpg”] placeholderImage:[UIImage imageNamed:@“placeholder.png”]];

  • +0

    对没错,我使用SDWebImage下载和缓存图像 –

    +0

    @danhü可以给我一个解决方案时,答案我正在测试我的代码小数据我没有面对这个问题,当数据变得很大这个错误出现 –

    +0

    当你快速滚动图像正在下载,他们只是彼此混乱,因为图像正在下载异步队列,使用同步队列或发送我的代码将发送给您的工作副本 谢谢 – Optimus

    0

    像SDWebImage这样的第三方库是一个合理的想法,虽然简单的家庭版本并不是太难以创建。相反,在细胞常规图像视图,使用自己的子类,像这样:

    // .h 
    #import <UIKit/UIKit.h> 
    @interface AsynchImageView : UIImageView 
    - (void)setImageFromUrl:(NSString *)url placeholder:(UIImage *)placeholder; 
    @end 
    
    // .m 
    #import "AsynchImageView.h" 
    
    @implementation AsynchImageView 
    
    - (void)setImageFromUrl:(NSString *)url placeholder:(UIImage *)placeholder { 
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; 
        NSURLCache *cache = [NSURLCache sharedURLCache]; 
        NSCachedURLResponse *cachedResponse = [cache cachedResponseForRequest:request]; 
        if (cachedResponse) { 
         [self setImageFromData:cachedResponse.data]; 
        } else { 
         self.image = placeholder; 
         NSURLSession *session = [NSURLSession sharedSession]; 
         [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
          if (!error) { 
           [cache storeCachedResponse:[[NSCachedURLResponse alloc] initWithResponse:response data:data] forRequest:request]; 
           [self setImageFromData:data]; 
          } 
         }] resume]; 
        } 
    } 
    
    - (void)setImageFromData:(NSData *)data { 
        UIImage *image = [UIImage imageWithData:data]; 
        dispatch_async(dispatch_get_main_queue(), ^{ 
         self.image = image; 
        }); 
    } 
    
    @end 
    

    它检查是否已将图像下载,若没有它设置一个占位符图像,并开始下载。完成后,它将图像设置为下载的图像并将其缓存。

    构成单元时,删除所有其他图像的代码,而做到这一点:

    NSString *url = currentProduct.mirsaProductImage; 
    [cell.mirsaProductLogoImageView setImageFromUrl: placeholder:];