我有两个集合视图单元格A和B,我需要同时加载这些单元格。但我没有找到任何解决方案
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 :(得分: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
}
答案 1 :(得分:0)
在CustomCollectionViewCell
viewDidLoad:
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!
}