为什么collectionViewCells不在iOS中显示?
您好,我可以从gallery中获取图像,但是collectionViewCells没有显示在UI上。我拖放collectionView提供了到委托和数据源方法的连接。但是为什么没有获取collectionViewCells。我编写了这样的代码为什么collectionViewCells不在iOS中显示?,ios,uicollectionview,Ios,Uicollectionview,您好,我可以从gallery中获取图像,但是collectionViewCells没有显示在UI上。我拖放collectionView提供了到委托和数据源方法的连接。但是为什么没有获取collectionViewCells。我编写了这样的代码 - (void)viewDidLoad { imagesArray = [NSMutableArray array]; [self.collectionView registerNib:[UINib nibWithNibName:@"MyCell"
- (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;
}
请任何人向我建议如何执行此操作。非常感谢您的帮助请确保您有带数据的
imageArray
。并尝试在imagePickerController:didFinishPickingMediaWithInfo:
方法中重新加载collectionView
- (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];
[self.collectionView reloadData];
[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];
return cell;
}
问题是您正在调用cellForItemAtIndexPath中的重载数据。这将导致无限循环。是否将单元格设置为xib文件,而不是脚本?将recipeImageView的子视图添加到单元格中。[cell.contentview addsubview:recipeImageView];我制作了cell in.xib rdelmarIs recipeImageView如果你记录它的话会显示为非零吗?是的,它显示为非零..我可以以NSMutableArrays的形式获取图像,谢谢你给出了很好的答案rdelmar..现在工作了