ios - 带有 UICollectionView 的 xib - 不符合键值编码

标签 ios swift uiview uicollectionview xib

我有一个带有 XIB 的自定义 UIView。这个自定义 UIView 有一个 UICollectionView 连接到一个 IBOutlet。在 View 设置中,UICollectionView 已正确初始化并且不是 nil。

但是在 cellForItemAtIndexPath 方法中,我得到这个错误:-

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key selectorCollectionView.'

如果我删除数据源和委托(delegate),我不会收到任何错误。如果我添加它们,我会在这一行出现错误:-

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "selectorCell", for: indexPath) as! SelectorCell

请帮忙!

编辑:我附上了我的设置截图

The custom view is added to a UITableViewCell

The custom view has this collectionview

I have set the class for the cell

and the identifier

我也在这里上传了项目http://www.fast-files.com/getfile.aspx?file=148623

最佳答案

可能是同步问题。有时会发生:

  1. 尝试松开 socket 并重新连接。

  2. 确保在 xib 文件中定义了 Collection Reusable View 标识符: Set identifier here

  3. 确保 Collection View 单元格的自定义类在 xib 文件中定义:

Set custom class here

编辑: 我深入研究了您的项目,这是我的发现

UICollectionView 应该在 awakeFromNib 方法中初始化(覆盖它):

override func awakeFromNib() {
    super.awakeFromNib()
    let cellNib = UINib(nibName: String(describing: "SelectorCell"), bundle: nil)
    selectorCollectionView.register(cellNib, forCellWithReuseIdentifier: "selectorCell")
    selectorCollectionView.dataSource = self
    selectorCollectionView.delegate = self
}

loadViewFromNib 应该看起来像这样(删除 Collection View 初始化'):

func loadViewFromNib() -> UIView {
    let nib = UINib(nibName: "SelectorView", bundle: nil)
    let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
    return view
}

同样是 SelectorTableViewCell 的子类。

class SelectorTableViewCell: UITableViewCell {
    @IBOutlet weak var selectorView: SelectorView!
}

Main.storyboard 中 - 自定义类 UITableViewCellSelectorTableViewCell 并连接 SelectorView inside contentView 到“SelectorTableViewCell”的导出。

我想就是这样。 Here是项目:

关于ios - 带有 UICollectionView 的 xib - 不符合键值编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47183879/

相关文章:

swift - 由于没有 subview ,无法使用 Xib 文件创建自定义 View

iOS应用程序在后台被杀死 - 如何调试?

ios - 在 Swift 中使用自动布局时获取错误的 tableVIew.contentSize

ios - 在 iOS 上获取星期几的名称

ios - 如何在 UIView(不是 UITableView、UICollectionView、UIScrollView)上添加拉动刷新

swift - 如何检测多行字符串中每一行的第一个索引?

iOS - 核心动画 - View 在意外时发生变化

ios - 子类中 self 和 super 之间的混淆

iphone - 在我的 UITableView 上定位横幅时出现问题

ios - 将文件 url 从 documentDirectory 加载到 TableView