为什么 collectionView 单元格没有显示在 iOS 中?
why collectionViewCells are not displaying in iOS?
你好,我可以从图库中获取图像,但 collectionViewCells 未显示在 UI.i 上拖放 collectionView 提供了与委托和数据源的连接 methods.but 为什么没有得到 CollectionViewCells.I 有写成这样的代码
- (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;
}
请有人建议我怎么做this.help 不胜感激
确保您有 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 中调用了 reloadData。这将导致无限循环。
你好,我可以从图库中获取图像,但 collectionViewCells 未显示在 UI.i 上拖放 collectionView 提供了与委托和数据源的连接 methods.but 为什么没有得到 CollectionViewCells.I 有写成这样的代码
- (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;
}
请有人建议我怎么做this.help 不胜感激
确保您有 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 中调用了 reloadData。这将导致无限循环。