【问题标题】:swift, How to use UICollectionView快速,如何使用 UICollectionView
【发布时间】:2018-08-13 04:40:03
【问题描述】:

我已经写了以下代码:

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 xxxx[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 扩展到类。 不能这样展开吗?

【问题讨论】:

  • 错误是什么?
  • 你没有提到错误
  • 我猜你忘了在 viewdidload 中写 collectionview.delegate = self 和 collectionview.datasource = self
  • @MuhammedAzharudheen 不会产生错误,我认为只会显示空集合。
  • 您能解释一下您所面临的错误吗?我没有发现上述代码有任何错误。

标签: ios swift uicollectionview


【解决方案1】:

将您的 CellReuseIdentifier 字符串添加到集合单元格(蓝色矩形)。参考下图

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    相关资源
    最近更新 更多