模拟器中的黑色窗口 - 集合视图
问题描述:
该代码是一个简单的集合视图与一堆图像 我运行它和Xcode不会争论任何事情,但模拟器在运行应用程序后显示黑屏。模拟器中的黑色窗口 - 集合视图
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return recipePhotos.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier [email protected]"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
// Configure the cell
UIImageView * recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image=[UIImage imageNamed:[recipePhotos objectAtIndex:indexPath.row]];
return cell;
}
答
- 检查集合视图的数据源出口,也许它不是你的控制器连接?
- 检查receipeImageView不是零。
- 您是否使用标识符@“Cell”注册您的UICollectionViewCell?
+0
是的,我做到了,但不是关于单元注册。 问题通过注释掉Xcode默认提供的一行来解决。不管怎样,谢谢你 – Light
答
如果你不希望黑色的背景呢,你必须设置
collectionView.backgroundColor = [UIColor clearColor];
然后,你必须注册细胞
UINib *cellNib = [UINib nibWithNibName:@"yournibname" bundle:nil];
[collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cell"];
两者都需要在viewDidLoad方法中实现
UIImage * recImage = [UIImage imageNamed:[recipePhotos objectAtIndex:indexPath.row]]; –
首先检查这个非空 –
是否在viewdidload中注册了单元格? –