正如标题所示,我有一个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(用于UI的原因)中调用getPhotos方法,从服务中获取照片,然后调用[imagCcollectionView reloadData];里面的getPhotos.
然后在cellForItemAtIndexPath中,我执行以下操作:
imageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath]; GRKAlbum *album = [albums objectAtIndex:indexPath.item]; cell.titleLabel.text = album.name; return cell;
我知道这里没有做错什么,因为这个方法是被调用的,单元格不是零,也不是专辑.
所以,当我运行所有这些代码,UICollectionView出现,但没有任何内容.我可以看到的是collectionView的背景颜色.任何帮助都将不胜感激.
注意:当我创建了ImageCell类时,我并没有创建一个XIB,而是使用了Storyboards.
解决方法
有一些方法来调试这种情况.
>注册UICollectionViewCell类而不是您的customCell,并在cellForItem中设置单元格的背景颜色…
>如果单元格出现在步骤1中,尝试使用您自己的单元格类并设置其背景
你在使用故事板吗?然后..
您不必为集合视图注册任何单元格.您可以使用原型单元格,只需为单元格提供正确的ID.您可以在原型单元格中设计所有内容.在所有情况下都不需要自定义类.