2017-03-09 44 views
0

我从一个很长的时间进行恢复,这是以前的工作应用程序,所以我假设它的下跌在新版本的iOS操作系统的变化。图像UICollectionView不显示从NSMutableArray的

当我在模拟器上运行应用程序时,没有显示图像,但正在创建单元并显示名称。我知道我必须在某处失去一些东西,但不能在我的生活中看到它,但是我不熟悉最近的变化。

任何帮助深表感谢,这是我的代码:

- (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"]; 
     } 
    } 
} 
+2

你可以试试,看看imageNamed返回的实际图像,也许有事与资产,就可以加载名称那些图像,别的地方? – JackRobinson

+0

看起来有可能您使用的文件名不是资产名称。 – torinpitchers

+0

您的图片在资产文件中?然后尝试删除'.jpg'扩展名。 @JackRobinson可能是对的,你的图片可能不会被加载。 – vitormm

回答

0

解决了!

试图对图像文件夹进行过于严格的组织,然后对每个类别使用子文件夹。

当我将所有从子文件夹移动到图像文件夹时,他们都开始显示!

感谢您的帮助!

+0

@JackRobinson感谢让我检查我很喜欢这么多的代码,它只是在移动文件后必须重新编制索引它,它工作...许多小时的挫折治好了大声笑 – David

0

你建立在故事板项目?关于您创建的UICollectionViewCell以及graniteImageView从 [cell viewWithTag:100]返回的调试信息是什么? 请确保您将graniteImageView添加到单元格并且具有正确的框架,请检查您的单元格并且图像是否有效。

0

我得到了相同的issue.And我的问题是这样,我创建的UIImageView编程

的UIImageView * graniteImageView = [[UIImageView的页头] initWithFrame使用下面code.In解决: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];