Ios 在Collectionview中添加多个CollectionViewCell.xib

Ios 在Collectionview中添加多个CollectionViewCell.xib,ios,uicollectionview,switch-statement,Ios,Uicollectionview,Switch Statement,实际上,我想将多个xib文件添加到一个UIcollectionview。我有一个switch case来为不同的indepath分配不同的collectionviewcell。我没有任何明确的想法如何使之成为可能 CollectionCell *cell1; switch (indexPath.row) { case 0: [self.Collection registerNib:[UINib nibWithNibName:@"CCollectionCe

实际上,我想将多个xib文件添加到一个
UIcollectionview
。我有一个switch case来为不同的
indepath
分配不同的
collectionviewcell
。我没有任何明确的想法如何使之成为可能

CollectionCell *cell1;
 switch (indexPath.row) {
        case 0:
            [self.Collection registerNib:[UINib nibWithNibName:@"CCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"board"];
            cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"dashboard" forIndexPath:indexPath];
            return cell1;
            break;
}

您仍然需要使用switch case或if else来指定要加载的单元格,但这些是一些可以更新的更改,以缩短代码

  • 将寄存器移动到
    viewDidLoad
    ,只需调用一次:
  • -(void)viewDidLoad{
    [超级视图下载];
    [collection Registernb:[UINib nibWithNibName:@“CCollectionCell”捆绑包:nil]forCellWithReuseIdentifier:@“board”];
    }

  • break
    将永远不会被调用,因此您也可以将其删除

  • 您仍然需要使用switch case或if else来指定要加载的单元格,但这些是一些可以更新的更改,以缩短代码

  • 将寄存器移动到
    viewDidLoad
    ,只需调用一次:
  • -(void)viewDidLoad{
    [超级视图下载];
    [collection Registernb:[UINib nibWithNibName:@“CCollectionCell”捆绑包:nil]forCellWithReuseIdentifier:@“board”];
    }

  • break
    将永远不会被调用,因此您也可以将其删除

  • 您的代码样式不起作用,因为
    indexath.row
    不是swift的固定参数,所以请执行如下代码:

    viewDidLoad

     [self.collectionView registerNib:[UINib nibWithNibName:@"CustomCell0" bundle:nil] forCellWithReuseIdentifier:@"MyCell0"];
    
     [self.collectionView registerNib:[UINib nibWithNibName:@"CustomCell1" bundle:nil] forCellWithReuseIdentifier:@"MyCell1"];
    // do same for all your six xib
    
    numberofItems

    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
        return 6;
    }
    
    cellForItem

    if(indexpath.row == 0){
       CustomCell0 *cell0 = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell0" forIndexPath:indexPath];
    
           return cell0;
        }
    else if(indexpath.row == 1){
        CustomCell1 *cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell1" forIndexPath:indexPath];
    
           return cell1;
        }
    
        :
        :
    
    else if(indexpath.row == 4){
        CustomCell4 *cell4 = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell4" forIndexPath:indexPath];
    
          return cell4;
        }
    else
        {
        CustomCell5 *cell5 = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell5" forIndexPath:indexPath];
    
           return cell5;
        }
    

    您的代码样式不起作用,因为
    indexath.row
    不是swift的固定参数,所以请执行如下代码:

    viewDidLoad

     [self.collectionView registerNib:[UINib nibWithNibName:@"CustomCell0" bundle:nil] forCellWithReuseIdentifier:@"MyCell0"];
    
     [self.collectionView registerNib:[UINib nibWithNibName:@"CustomCell1" bundle:nil] forCellWithReuseIdentifier:@"MyCell1"];
    // do same for all your six xib
    
    numberofItems

    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
        return 6;
    }
    
    cellForItem

    if(indexpath.row == 0){
       CustomCell0 *cell0 = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell0" forIndexPath:indexPath];
    
           return cell0;
        }
    else if(indexpath.row == 1){
        CustomCell1 *cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell1" forIndexPath:indexPath];
    
           return cell1;
        }
    
        :
        :
    
    else if(indexpath.row == 4){
        CustomCell4 *cell4 = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell4" forIndexPath:indexPath];
    
          return cell4;
        }
    else
        {
        CustomCell5 *cell5 = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell5" forIndexPath:indexPath];
    
           return cell5;
        }
    

    我刚刚给出了第0th indexpath.row的一个示例。我已经给出了我需要的案例。我想在“//your cell for row 0”中给出代码。告诉我你的条件,比如你有多少个xib,你想按什么顺序显示它们,哪个xib显示多少次?我总共有6个xib,在每个indexpath中,我必须给出这个xib。所以我在swithcase中总共有6个案例,当我给出添加collectionviewcells的代码时,我的代码Crassesi只是给出了第0个indexpath.row的示例。我已经给出了我需要的案例。我想在“//your cell for row 0”中给出代码。告诉我你的条件,比如你有多少个xib,你想按什么顺序显示它们,哪个xib显示多少次?我总共有6个xib,在每个indexpath中,我必须给出这个xib。因此,我在swithcase中总共有6个案例,当我给出添加collectionviewcells的代码时,我的代码库中有超过1个collectionviewcell.xib,那么我应该为所有xib文件编写上述代码吗?是,您必须注册集合视图将使用的所有nib,并且每个xib文件只注册1次我有多个collectionviewcell.xib那么我应该为所有xib文件编写上述代码吗?是的,您必须注册集合视图将使用的所有nib,并且每个xib文件只注册1次