嗨,我可以从图库中获取图像,但collectionViewCells不在UI上显示。我将collectionView拖放到委托和datasourse方法的连接上,但是为什么没有得到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;
}

请有人建议我该怎么做。帮助表示赞赏

最佳答案

确保您的数据带有imageArray。并尝试以collectionView方法重新加载imagePickerController:didFinishPickingMediaWithInfo:

- (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;
}

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

10-10 14:21