在customCell中循环遍历uiimageview

Zak*_*ish 10 objective-c uiimageview ios

您好,我有每个产品的产品表和产品详细信息视图我有许多图像下载并缓存在磁盘和内存上每次用户选择产品检查其详细信息我正在尝试检索缓存的图像并为我的四个设置它在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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果我试图改变循环方式到这个

 for ( UIImageView *currentImageView in self.tableView.subViews)
Run Code Online (Sandbox Code Playgroud)

我收到此错误UITableViewWrapperView setImage:]:无法识别的选择器发送到实例

我只是想知道我是否以错误的方式循环我的意思是我无法通过这个循环或正在发生的事情到达他们?

小智 7

您必须将循环更改为:

 for ( UIView *currentView in cell.contentView.subviews)
   {
      if([currentView isKindOfClass:[UIImageView class]])
      {
           // here Do All the stuff for imageViews
      }
   }
Run Code Online (Sandbox Code Playgroud)

做所有这些东西cellForRowAtIndexPath 和更改imageView图像采取imageView的弱实例,如下所示:

__weak UIImageView *weakImage = currentImageView;
 [self.imageCache queryDiskCacheForKey:self.currentProduct.mirsaProductUrlImage[indexOfImages]  
    done:^(UIImage *image, SDImageCacheType cacheType) {
                if (image) {
                    weakImage.image =  image;
                }
 }];
Run Code Online (Sandbox Code Playgroud)