我们如何为单个 UICollectionView 使用两种类型的 UICollectionViewCell?

How can we use two types of UICollectionViewCell for a single UICollectionView?

我有两种类型的 UICollectionViewCell :一种是画廊单元格,另一种是食谱单元格。

有一个切换按钮。 第一次加载视图时,图库单元格用于在集合视图中显示。

但是在单击切换按钮时,我尝试加载另一个单元格:(配方单元格)但它崩溃了,说没有 属性(实际上这是配方单元格的 属性)图库单元格。

有没有办法用另一个单元格加载相同的集合视图?

提前致谢。

编辑:

    if (isGalleryOnCollectionView) {

    ProfileFollowerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell"  forIndexPath:indexPath];

    NSString *followerName;

    if ([[arr_followerNames objectAtIndex:indexPath.row] isEqualToString:@""]) {
        followerName=[arr_followersEmail objectAtIndex:indexPath.row];
    }
    else{
        followerName=[arr_followerNames objectAtIndex:indexPath.row];
    }

    NSLog(@"\n\n\n follower name is :: %@\n\n",followerName);

    cell.lbl_followerName.text = followerName;

}else{

        GalleryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];


        cell.lbl_recipeName.text = [[[dict_profileData valueForKey:@"recipes"] valueForKey:@"name"] objectAtIndex:indexPath.row];


}

解决方案:

[self.profleCollectionView registerClass:[ProfileFollowerCell class] forCellWithReuseIdentifier:@"followercell"];

在从 nib 注册之后,在将可重用标识符出列之前,它可以工作。

Here is a good tutorial on creating custom UICollectionViewCell with xibs.

你缺少的是 -(void)viewDidLoad

UINib *cellNib = [UINib nibWithNibName:@"ProfileFollowerCellNib" bundle:nil];
[self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"TheNibCellIdentifier"];

您还需要确保在加载单元格时使用相应的 ReuseIdentifier

ProfileFollowerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TheNibCellIdentifier"  forIndexPath:indexPath];