【问题标题】:Multiple CollectionViews in one ViewController一个 ViewController 中的多个 CollectionView
【发布时间】:2020-08-28 15:58:48
【问题描述】:

我尝试在一个 ViewController 中显示多个集合视图,但我因错误而崩溃:

Thread 1: Exception: "could not dequeue a view of kind: UICollectionElementKindCell with identifier MainCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard"

我在 Storyboard 中注册了所有单元格,这是我的 ViewController 代码:

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout  {

    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var horizontalNumbers: UICollectionView!
    @IBOutlet weak var verticalNumbers: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionView.delegate = self
        collectionView.dataSource = self

        horizontalNumbers.delegate = self
        horizontalNumbers.dataSource = self

        verticalNumbers.delegate = self
        verticalNumbers.dataSource = self
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        if collectionView == collectionView {
           return 81
        } else {
           return 9
        }
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        if collectionView == collectionView {
           let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MainCell", for: indexPath) as! CollectionViewCell
           cell.backgroundColor = .systemGreen
           return cell
        } else if collectionView == horizontalNumbers {
           let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Horizontal", for: indexPath) as! HorizontalNumbersCell
           return cell
        } else {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Vertical", for: indexPath) as! VerticalNumbersCell
            return cell
        }
    }
 
}

我检查了所有内容并查找了一些代码示例,但没有公布我崩溃的原因。

【问题讨论】:

    标签: ios swift uicollectionview viewcontroller


    【解决方案1】:

    您需要为所有三个collectionView注册单元格。以collectionView为例。

    试试

    collectionView.register(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "MainCell") // if it is loaded in nib, and nib name is CollectionViewCell
    

    collectionView.register(CollectionViewCell.self, forCellWithReuseIdentifier: "MainCell") // if it is written just in code
    

    viewDidLoad

    horizontalNumbers等做同样的事情。

    查看https://developer.apple.com/documentation/uikit/uicollectionview/1618089-register了解更多详情。

    【讨论】:

    • 我添加到 viewdidload: collectionView.register(CollectionViewCell.self, forCellWithReuseIdentifier: "MainCell") Horizo​​ntalNumbers.register(VerticalNumbersCell.self, forCellWithReuseIdentifier: "Horizo​​ntal") verticalNumbers.register(Horizo​​ntalNumbersCell.self, forCellWithReuseIdentifier :“垂直”),但无论如何都会因错误而崩溃:线程 1:异常:“无法使类型视图出列:带有标识符 MainCell 的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或一个类或连接故事板中的原型单元"
    • 那我建议你在 Github 上上传你的项目。根据我自己的经验,我无法仅通过您发布的代码来判断发生了什么。
    猜你喜欢
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 2017-10-03
    • 2013-08-24
    • 2016-07-26
    相关资源
    最近更新 更多