[英]How can I add multiple collection views in a UIViewController in Swift?
[英]How can i load multiple custom views in collection view swift
我有两个集合视图单元格 A 和 B ,我需要同时加载这些单元格。 但我没有找到任何解决方案
firstCollectionView.register(UINib(nibName: "A", bundle: Bundle.main), forCellWithReuseIdentifier: "A")
firstCollectionView.register(UINib(nibName: "B", bundle: Bundle.main), forCellWithReuseIdentifier: "B")
这是两个视图以及如何一次加载两个视图。
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "A", for:indexPath) as? A
你想如何划分不同的细胞类型? 与号码? 比如,如果 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
}
在viewDidLoad:
注册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!
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.