在我的集合视图中,对于数组中不同类型的数据,单元格类必须具有完全不同的外观。
我正在寻找一种创建多个单元格的方法,并根据输入数据选择不同的单元格,例如:
internal func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell1 = collectionView.dequeueReusableCell.. as! kind1
let cell2 = collectionView.dequeueReusableCell.. as! kind2
// here choose a different one for each kind of data
return cell1
}
我想了解是否:
答案 0 :(得分:5)
你需要做这样的事情
internal func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if (data.kind == kind1) {
let cell1 = collectionView.dequeueReusableCell.. as! kind1
return cell1
} else {
let cell2 = collectionView.dequeueReusableCell.. as! kind2
return cell2
}
}
通过检查数据的类型,您可以确定单元格。
答案 1 :(得分:1)
我只想提及这一点,对于任何可能想知道如何做到这一点的未来用户。现在,我认为使用indexPath.item
并检查它是否为单元格1或单元格2要容易得多。这是一个例子
if indexPath.item == 0
{
// Cell 1
let cell1 = let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell1", for: indexPath) as! Cell1
return cell1
}
else
{
// Cell 2
let cell2 = let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell2", for: indexPath) as! Cell1
return cell2
}
如果您有超过2个自定义UICollectionViewCells
,则只需添加else if语句并检查其indexPath.item
。
答案 2 :(得分:0)
Swift 5示例,根据需要进行更改。
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(collectionView)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
collectionView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
collectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
registerCells()
}
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 0
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = .white
cv.dataSource = self
cv.delegate = self
cv.isPagingEnabled = true
return cv
}()
var cellId = "Cell"
var celltwoCellId = "CellTwoCell"
fileprivate func registerCells() {
collectionView.register(CellOneCell.self, forCellWithReuseIdentifier: cellId)
collectionView.register(CellTwoCell.self, forCellWithReuseIdentifier: celltwoCellId)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 0
{
// Cell 1
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! CellOne
return cell
}
else
{
// Cell 2
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: celltwoCellId, for: indexPath) as! CellTwo
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: view.frame.height)
}
}