嗨,我可以从图库中获取图像,但 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/