【发布时间】:2021-12-14 17:05:16
【问题描述】:
class BaseUICollectionViewCell: UICollectionViewCell { // -> This comes from XIB
func method() {}
}
class SubClassUICollectionViewCell: BaseUICollectionViewCell { // -> Overrides some of its super class methods
override func method() {}
}
如何在我的收藏视图中同时使用它们?
在您回答之前,请确保您创建了一个项目并测试我在这里解释的内容!我需要针对此处提到的当前案例的解决方案!
class ViewController: UIViewController {
@IBOutlet var collectionView: UICollectionView!
public override func viewDidLoad() {
super.viewDidLoad()
collectionViewSetup()
}
private func collectionViewSetup() {
collectionView.delegate = self
collectionView.dataSource = self
// What is the right thing overe here
let xib = UINib(nibName: "BaseUICollectionViewCell", bundle: Bundle(for: Self.self))
collectionView.register(xib, forCellWithReuseIdentifier: "BaseUICollectionViewCell")}
}
}
// MARK: - UICollectionViewDataSource
extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate {
public func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 2
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// what is the right thing over here
if indexPath.row %2 == 0 {
// BaseUICollectionViewCell
} else {
// SubClassUICollectionViewCell
}
return UICollectionViewCell()
}
}
【问题讨论】:
标签: swift uicollectionview uicollectionviewcell xib