如标题所示,我有一个 UICollectionView
,它通过 IB 连接到一个 UIViewController
。我有一个名为 imageCell
的 UICollectionView
子类。在 viewcontroller viewDidLoad
方法中,我这样注册类:
CGRect collectionViewRect = CGRectMake(384, 60, 728, 924);
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
imageCollectionView = [[UICollectionView alloc] initWithFrame:collectionViewRect collectionViewLayout:flow];
[imageCollectionView registerClass:[imageCell class] forCellWithReuseIdentifier:@"Cell"];
imageCollectionView.delegate = self;
imageCollectionView.dataSource = self;
然后我在 viewDidAppear
中调用一个名为 getPhotos
的方法(出于 UI 原因)从服务获取照片,然后我调用 [imagCcollectionView reloadData];
在 getPhotos
中。
然后在 cellForItemAtIndexPath
中,我执行以下操作:
imageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
GRKAlbum *album = [albums objectAtIndex:indexPath.item];
cell.titleLabel.text = album.name;
return cell;
我知道我在这里没有做错任何事情,因为正在调用该方法并且 cell
不是 nil
也不是 album
。
因此,当我运行所有这些代码时,UICollectionView
出现了,但其中什么也没有。我只能看到 collectionView
的背景色。任何帮助都将不胜感激。
注意:我创建时并没有为 imageCell
类创建 XIB,我使用的是 Storyboard。
最佳答案
有一些方法可以调试这种情况。
注册 UICollectionViewCell 类而不是您的 customCell 并在
cellForItem...
中设置单元格的背景颜色如果单元格出现在第一步中,请尝试使用您自己的单元格类并设置其背景
你在使用 Storyboard吗?然后..
您不必为 Collection View 注册任何单元格。您可以使用原型(prototype)单元,只需为单元提供正确的 ID。您可以在原型(prototype)单元中设计所有内容。在所有情况下可能都不需要自定义类。
关于ios - UICollectionViewCells 未出现在 UICollectionView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16072891/