【问题标题】:looping through uiimageview in customCell在customCell中循环uiimageview
【发布时间】:2017-05-01 21:06:48
【问题描述】:

您好,我有每个产品的产品表和产品详细信息视图,每次用户选择产品以检查其详细信息时,我都有许多图像被下载并缓存在磁盘和内存上,我正在尝试检索缓存的图像并进行设置对于我的四个 UIImageViewUITableViewController 的自定义单元格中,我无法设置图像,尽管我可以通过 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:]: unrecognized selector sent to instance

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

【问题讨论】:

  • 目前尚不清楚您实际上想要实现什么,但尝试访问您的 tableview 的子视图几乎可以肯定不是正确的方法。发生您的异常是因为并非所有表的子视图都是 UIImageViews。
  • 我的朋友我正在尝试访问 UITableView 的 customCell 中的所有 UIImageView 我该如何实现?
  • 是的,你说过,但不清楚为什么你会想做这样的事情。您的表格视图单元格只是您的数据模型的表示。您是否尝试使用已显示单元格后从网络检索到的缓存图像替换占位符图像?
  • @Paulw11 正是我的朋友,我知道我的英语太糟糕了:(我很抱歉
  • @Paulw11 是的,我正在努力做你想说的?有什么解决方案吗?

标签: ios objective-c uiimageview


【解决方案1】:

您必须将循环更改为:

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

【讨论】:

  • 正是我所做的
  • 将 imageview 实例更改为 weak 将占用更少的内存。
【解决方案2】:

我认为这不适用于您的代码。 作为

for ( UIImageView *currentImageView in cell.contentView.subviews)

此行将尝试将单元格内容视图的所有子视图转换为 UIImageview。

您可以为所有 UIImageview 设置标签,然后使用标签直接访问它们

cell.contentView.viewWithTag(yourtag)

并为其设置图像

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    相关资源
    最近更新 更多