【发布时间】: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