你好,大家好我从我的服务器下载每个产品的图片并进行设置,我的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
对没错,我使用SDWebImage下载和缓存图像 –
@danhü可以给我一个解决方案时,答案我正在测试我的代码小数据我没有面对这个问题,当数据变得很大这个错误出现 –
当你快速滚动图像正在下载,他们只是彼此混乱,因为图像正在下载异步队列,使用同步队列或发送我的代码将发送给您的工作副本 谢谢 – Optimus