【问题标题】:images not showing in UICollectionView from NSMutableArray图像未显示在 NSMutableArray 的 UICollectionView 中
【发布时间】:2017-03-09 11:43:43
【问题描述】:

我从很久以前回来,这是一个以前工作的应用程序,所以我假设它是由于新版本的 iOS 系统的变化。

当我在模拟器上运行应用程序时,没有显示图像,但正在创建单元格并显示名称。我知道我一定在某个地方遗漏了一些东西,但我一生都看不到它,但是我不熟悉最近的变化。

非常感谢任何帮助,这是我的代码:

- (void)createData {

    self.graniteImages = [NSMutableArray array];

    [self.graniteImages addObject:[[NSMutableDictionary alloc]
                                  initWithObjectsAndKeys:@"Angel Cream", @"name",
                                  @"angel-cream.jpg", @"image", nil]];
    [self.graniteImages addObject:[[NSMutableDictionary alloc]
                                  initWithObjectsAndKeys:@"Angola Black", @"name" ,
                                  @"angola_black1.jpg", @"image" , nil]];
    [self.graniteImages addObject:[[NSMutableDictionary alloc]
                                  initWithObjectsAndKeys:@"Angola Silver", @"name" ,
                                  @"angola-silver1.jpg", @"image" , nil]];

    [self.collectionView reloadData];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.graniteImages.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    UIImageView *graniteImageView = (UIImageView *)[cell viewWithTag:100];
    graniteImageView.image = [UIImage imageNamed:[[self.graniteImages objectAtIndex:indexPath.row] objectForKey:@"image"]];

    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (_startingIndexPath) {
        NSInteger currentIndex = floor((scrollView.contentOffset.x - scrollView.bounds.size.width / 1) / scrollView.bounds.size.width) + 1;
        if (currentIndex < [self.graniteImages count]) {
            self.title = self.graniteImages[currentIndex][@"name"];
        }
    }
}

【问题讨论】:

  • 您可以尝试查看 imageNamed 是否返回实际图像,可能资产出现问题,您可以按名称加载这些图像吗?
  • 看起来您使用的可能是文件名而不是资产名称。
  • 您的图片在资产文件中?然后尝试删除“.jpg”扩展名。 @JackRobinson 可能是对的,您的图像可能没有被加载。
  • 你是否设置了uiImageView的标签,并且图像名称应该在你的资产或捆绑中相同。

标签: ios objective-c uiimageview uicollectionview


【解决方案1】:

您是在情节提要中构建项目吗?您创建的有关UICollectionViewCell 的调试信息是什么以及从返回的graniteImageView [cell viewWithTag:100]? 确保您将graniteImageView 添加到单元格并具有正确的框架,检查您的单元格和图像是否有效。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。我的问题已使用以下代码解决。在此我以编程方式创建 UIImageView

    UIImageView *graniteImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];

    graniteImageView.image = [UIImage imageNamed:[[self.graniteImages objectAtIndex:indexPath.row] objectForKey:@"image"]];
    
    graniteImageView.contentMode = UIViewContentModeScaleAspectFit;
    
    graniteImageView.tag = indexPath.row;// Add here what tag you need
    
    [cell addSubview:graniteImageView];
    

    【讨论】:

      【解决方案3】:

      解决了!

      试图用图像文件夹和每个类别的子文件夹来组织得过于井井有条。

      当我将所有子文件夹中的所有内容移动到刚刚开始显示的图像文件夹中时!

      感谢大家的帮助!

      【讨论】:

      • @JackRobinson 感谢您让我检查我是否喜欢这么多代码,它只是在移动必须重新索引它们的文件之后才起作用......许多小时的挫败感治愈了大声笑
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-24
      • 2021-01-21
      • 1970-01-01
      • 2019-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多