我写了以下代码:
class HomeViewController: UIViewController {
@IBOutlet var collectionView: CatagoryCollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
collectionView.register(UINib(nibName: "HomeCardCell",bundle: Bundle.main), forCellWithReuseIdentifier: "HomeCardCell")
}
}
class CatagoryCollectionView: UICollectionView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCardCell", for: indexPath) as! HomeCardCell
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
}
}
我在日志中有此错误:
2018-08-13 12:14:07.706452 + 0900 xxxxx [6804:3378858] -[ID301Project.HomeViewController collectionView:numberOfItemsInSection:]:无法识别的选择器发送到 实例0x1065092b0 2018-08-13 12:14:07.707910 + 0900 xxxxx [6804:3378858] ***由于未捕获的异常而终止应用程序 “ NSInvalidArgumentException”,原因:“-[xxxxx.HomeViewController collectionView:numberOfItemsInSection:]:无法识别的选择器发送到 实例0x1065092b0'
我想将UICollectionView扩展到类。 您不能像这样扩展它吗?