2017-02-13 63 views
1

我有两个集合视图单元格A和B,我需要同时加载这些单元格。但我没有找到任何解决方案我如何加载集合视图中的多个自定义视图swift

  firstCollectionView.register(UINib(nibName: "A", bundle: Bundle.main), forCellWithReuseIdentifier: "A") 
    firstCollectionView.register(UINib(nibName: "B", bundle: Bundle.main), forCellWithReuseIdentifier: "B") 

这是两个观点,以及如何可以在加载时间2次。

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "A", for:indexPath) as? A 
+0

在同一节或同细胞或任何其他情况? –

+0

你的意思是什么? ...你需要两个不同的细胞还是? – John

+1

您可以同时对A和B同时进行序列化,并选择要返回的哪一个 – Tj3n

回答

1

你想如何划分不同的细胞类型?数字?就像,如果raw = 0,2,4,6等,你将有firstCell,如果raw = 1,3,5等,你会有secendCell?

因此,也许有这样的事情:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = UICollectionViewCell() 

     cell = collectionView.register(UINib(nibName: "B", bundle: Bundle.main), forCellWithReuseIdentifier: "B") 

     if indexPath.row % 2 == 0 { 
      cell = collectionView..register(UINib(nibName: "A", bundle: Bundle.main), forCellWithReuseIdentifier: "A") 
     } 

     return cell 
    } 
0

注册CustomCollectionViewCellviewDidLoad:

var nib1 = UINib(nibName: "CustomCollectionViewCell1", bundle: nil) 
    self.firstCollectionView().registerNib(nib1, forCellReuseIdentifier: "CustomCell1") 
    var nib2 = UINib(nibName: "CustomCollectionViewCell2", bundle: nil) 
    self.firstCollectionView().registerNib(nib2, forCellReuseIdentifier: "CustomCell2") 

现在这里cellForItemAtIndexPath:方法返回你的细胞,

//As per your condition check cell index or section or any other your condition. 


if indexPath.row % 2 == 0 { 

     // Create an instance of CustomCollectionViewCell1 
    var cell: CustomCollectionViewCell1? = tableView.dequeueReusableCell(withIdentifier: "CustomCell1") 
     if self.cell == nil { 
      self.cell = CustomCollectionViewCell1(style: .subtitle, reuseIdentifier: "CustomCell1") 
     } 
    return cell! 

    }else{ 
     // Create an instance of CustomCollectionViewCell2 
    var cell: CustomCollectionViewCell2? = tableView.dequeueReusableCell(withIdentifier: "CustomCell2") 
     if self.cell == nil { 
      self.cell = CustomCollectionViewCell2(style: .subtitle, reuseIdentifier: "CustomCell2") 
     } 
    return cell! 
    } 
相关问题