ios - 为什么 iOS 中 CollectionView 单元格不显示?

标签 ios uicollectionview

嗨,我可以从图库中获取图像,但 CollectionViewCells 未显示在 UI 上。我拖放 CollectionView 提供了与委托(delegate)和数据源方法的连接。但为什么没有获取 CollectionViewCells。我编写了这样的代码

- (void)viewDidLoad  
{ 
imagesArray = [NSMutableArray array]; 
[self.collectionView registerNib:[UINib nibWithNibName:@"MyCell" bundle:nil] forCellWithReuseIdentifier:@"CELL"]; 
}   

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info  
{ 
inputImage= info[UIImagePickerControllerEditedImage]; [imagesArray addObject:inputImage];
[picker dismissViewControllerAnimated:YES completion:nil];
} 

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView; {
return 1;
} 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
return [imagesArray count];
} 

- (MyCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
if(!cell) { 
cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL"forIndexPath:indexPath];
}
recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image =[imagesArray objectAtIndex:indexPath.row];        
[collectionView reloadData];
return cell;
}

请有人建议我如何做到这一点。不胜感激

最佳答案

问题是您在 cellForItemAtIndexPath 中调用 reloadData。这将导致无限循环。

关于ios - 为什么 iOS 中 CollectionView 单元格不显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29002302/

相关文章:

ios - 如何通过 UICollectionViewController (Swift) 中的 didSelectItemAtIndexPath() 方法访问所有单元格

ios - 快速无限向上滚动 collectionView

ios - Collection View 单元格未正确加载图像

ios - UIImage View 重置/清空

ios - 支持 Storyboard 中的多种屏幕尺寸(3.5 和 4 英寸屏幕)

ios - 使用常用方法创建类

ios - iPhone 7/8 plus、iPhone 7/8s 和 iPhone X 上的 CollectionView 问题

ios - 键盘隐藏了 IOS 中的文本字段

ios - 如何获取带有时差的GMT格式

iphone - 单元格获取其前一个单元格的索引 - Tableview 中的 Collectionview