你好,我有每个产品的产品表和产品详细信息视图我有很多图像下载并缓存在磁盘和内存上每次用户选择产品检查其详细信息我试图检索缓存的图像并设置它对于我在 UITableViewController 中的自定义单元格内的四个 UIImageView ,我无法设置图像,尽管我可以通过 NSLog()功能进行记录我正在使用此代码:
int index = 0;
int indexOfImages = 1;
self.imageCache = [[SDImageCache alloc] initWithNamespace:@"menuImage"];
for ( UIImageView *currentImageView in cell.contentView.subviews)
{
NSLog(@"the imageindex is: %d",indexOfImages);
NSLog(@"the index is: %d",index);
NSLog(@"the count is: %lu",(unsigned long)[self.currentProduct.mirsaProductUrlImage count]);
if (index < [self.currentProduct.mirsaProductUrlImage count] ) {
[self.imageCache queryDiskCacheForKey:self.currentProduct.mirsaProductUrlImage[indexOfImages]
done:^(UIImage *image, SDImageCacheType cacheType) {
if (image) {
currentImageView.image = image;
}
}];
indexOfImages++;
index++;
}
else
{
break;
}
}
如果我尝试将循环方式更改为此
for ( UIImageView *currentImageView in self.tableView.subViews)
我收到此错误UITableViewWrapperView setImage:]:无法识别的选择器发送到实例
我只是想知道我是否以错误的方式循环我的意思是我无法通过此循环或正在发生的事情与他们联系?
答案 0 :(得分:7)
您必须将循环更改为:
for ( UIView *currentView in cell.contentView.subviews)
{
if([currentView isKindOfClass:[UIImageView class]])
{
// here Do All the stuff for imageViews
}
}
在cellForRowAtIndexPath
中做所有这些事情
并改变imageView图像采取imageView的弱实例,如下所示:
__weak UIImageView *weakImage = currentImageView;
[self.imageCache queryDiskCacheForKey:self.currentProduct.mirsaProductUrlImage[indexOfImages]
done:^(UIImage *image, SDImageCacheType cacheType) {
if (image) {
weakImage.image = image;
}
}];
答案 1 :(得分:0)
我认为这不适用于您的代码。 如
for(UIImageView * currentImageView in cell.contentView.subviews)
此行将尝试将您单元格内容视图的所有子视图转换为UIImageview。
您可以将标记设置为所有UIImageview,然后使用
标记直接访问它们cell.contentView.viewWithTag( yourtag )
并将图像设置为