【问题标题】:Could not dequeue a view of kind: UICollectionElementKindCell after going back from background从后台返回后,无法出列同类视图:UICollectionElementKindCell
【发布时间】:2017-03-07 16:00:38
【问题描述】:

我正在 Swift3 中构建键盘扩展。

我的键盘第一次启动时,没问题,我的收藏视图显示得很好,一切正常。

当我的键盘进入后台并再次返回前台时会出现此错误。例如,我启动 iMessage,显示我的键盘,点击 iPhone 的“主页按钮”,再次启动 iMessage -> 崩溃:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法将类型视图出列:带有标识符 KeyboardViewCell 的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或一个类或连接故事板中的原型单元格”

这是我为单元格标识符设置故事板的方法:

使用正确的类:

这是我从cellForItemAt indexPath 方法中调用它的方式:

 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! KeyboardViewCell
        return cell

我不明白为什么在更换键盘后会发生崩溃,比如在那种情况下没有加载故事板或其他什么...我忘记了什么吗?

我的故事板是这样的:

【问题讨论】:

    标签: ios swift swift3 uicollectionviewcell custom-keyboard


    【解决方案1】:

    您的KeyboardViewController 拥有两个UICollectionViews。确保 MyCollectionView 在将 cellForItemAt indexPath 中的 KeyboardViewCell 出列时调用其 dataSource

    if collectionView === myCollectionView {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! KeyboardViewCell
        return cell
    } else {
        //dequeue soundCollectionViewCell
    }
    

    如果您为多个“委托者”(即多个ColletionView)遵守相同的协议,则必须在每个回调方法中实现此检查。

    【讨论】:

    • 感谢您的回复。这是个好主意,但它不起作用,因为它总是转到“else”
    • 这是您现在可以调试的另一个问题。
    • 遇到同样问题的朋友,别忘了在你的 numberOfItemsInSection 中添加这张支票!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 2017-11-22
    • 2016-02-22
    相关资源
    最近更新 更多