简体   繁体   English

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

[英]xib with UICollectionView - not key value coding compliant

I have a Custom UIView with an XIB.我有一个带有 XIB 的自定义UIView This custom UIView has a UICollectionView which is connected to an IBOutlet .这个自定义UIView有一个连接到IBOutletUICollectionView In the view setup, the UICollectionView is initialised properly and is not nil.在视图设置中, UICollectionView已正确初始化并且不为零。

However in the cellForItemAtIndexPath method, I get this error:-但是在cellForItemAtIndexPath方法中,我收到此错误:-

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key selectorCollectionView.'由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[ setValue:forUndefinedKey:]: 此类与键 selectorCollectionView 的键值编码不兼容。”

If I remove the datasource and delegate, I do not get any error.如果我删除数据源和委托,我不会收到任何错误。 If I add them Iget an error at this line:-如果我添加它们,我会在这一行出现错误:-

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

Please help!请帮忙!

Edit: I have attached screenshots of my setup编辑:我附上了我的设置截图

自定义视图添加到 UITableViewCell

自定义视图有这个 collectionview

我已经为单元格设置了类

和标识符

I have uploaded the project here too http://www.fast-files.com/getfile.aspx?file=148623我也在这里上传了项目http://www.fast-files.com/getfile.aspx?file=148623

Maybe a sync issue.可能是同步问题。 Happens sometimes:有时会发生:

  1. Try cut outlets loose and reconnect them.尝试将插座剪松并重新连接。

  2. Make sure Collection Reusable View identifier is defined in xib file:确保在 xib 文件中定义了集合可重用视图标识符: 在此处设置标识符

  3. Make sure collection-view cell's custom class is define in xib file:确保在 xib 文件中定义了集合视图单元格的自定义类:

在此处设置自定义类

EDIT: I dug into your project and here are my findings编辑:我深入研究了你的项目,这是我的发现

UICollectionView should be init' in awakeFromNib method (override it): 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 should be looking like this (Remove collection view init'): loadViewFromNib 应该是这样的(移除集合视图 init'):

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

Subclass SelectorTableViewCell likewise.子类 SelectorTableViewCell 同样。

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

In Main.storyboard - custom class UITableViewCell to SelectorTableViewCell and connect SelectorView inside contentView to 'SelectorTableViewCell''s outlet.Main.storyboard -自定义类UITableViewCellSelectorTableViewCell和连接SelectorView里面contentView到“SelectorTableViewCell”的出口。

That's it i think.我想就是这样。 Here is the project:是项目:

You can try the following:您可以尝试以下操作:

  1. Click on File Owner at the left panel of YourCell.xib .单击YourCell.xib左侧面板上的File Owner Then go to Attributes inspector and check whether there is any class specified in Class field.然后转到属性检查器并检查是否在Class字段中指定了任何类。 If not - no help from me here.如果没有 - 我在这里没有帮助。 If yes - remove it and tap Enter .如果是 - 删除它并点击Enter

  2. In YourCell.xib sequentially click on all subviews and delete all the outlets in Connections Inspector .YourCell.xib依次单击所有子视图并删除Connections Inspector 中的所有出口。 No need to delete them in YourCell.swift .无需在YourCell.swift删除它们。

  3. Once your cell's File Owner and outlets are removed, we're ready to reconnect the outlets.一旦您的单元的文件所有者和出口被删除,我们就可以重新连接出口。 Just drag every one as usually to an existing corresponding IBOutlet in YourCell.swift .只需像YourCell.swift一样将每一个拖到YourCell.swift现有的相应 IBOutlet 。

As a result, when you click on YourCell at the left panel of YourCell.xib and go to Connections Inspector , you'll see there are all the outlets connected.因此,当您单击YourCell.xib左侧面板上的YourCell.xib并转到Connections Inspector 时,您将看到所有插座都已连接。 Like so:像这样:

在此处输入图片说明

And if you check connections for a particular view in YourCell , you should see that it's outlet is connected to YourCell ( StaticSlot in my case).如果你检查连接在YourCell特定视图,你应该看到它的出口连接到YourCell(StaticSlot在我的情况)。

在此处输入图片说明

This helped me and hope you'll get the same.这对我有帮助,希望你也能得到同样的结果。

In our case, the problem was making the wrong connection in Storyboard.在我们的例子中,问题是在 Storyboard 中建立了错误的连接。 Click on the custom class in Storyboard -- not the File Owner -- then connect the IB outlet to the variable in the custom class.单击 Storyboard 中的自定义类——而不是文件所有者——然后将 IB 插座连接到自定义类中的变量。

if try to register you custom class and cell ID如果尝试注册您的自定义类和单元格 ID

self.collectionView.register(nib, forCellWithReuseIdentifier: "Cell")
self.collectionView.register(CustomClassCell.self, forCellWithReuseIdentifier: "Cell")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 加载 XIB 这个类不是键值编码兼容的键 - Load XIB this class is not key value coding-compliant for the key 不符合键值编码 - Not Key Value Coding Compliant 将项目从情节提要更改为Xib文件时,出现“此类不符合键的键值编码”错误 - “This class is not key value coding-compliant for the key” error when changing project from storyboard to xib files 删除情节提要并使用Xib会生成错误“类不符合键的键值编码要求” - Deleting Storyboard and using Xib generates error “class is not key value coding-compliant for the key” 来自XIB的新自定义单元格导致密钥值符合密钥...? - New custom cell from XIB causing key value coding-compliant for the key …? 从 xib 加载 UITableViewCell(该类不符合键值编码) - Loading UITableViewCell from a xib (this class is not key value coding-compliant for the key) 使用xib在新项目中始终会收到“不符合键值编码”错误 - Always get an error “not key value coding-compliant” on new project with xib 无法识别iOS Xib文件IBOutlet-(不符合键值编码的类) - iOS xib file IBOutlet not recognized - (class not key-value coding compliant) 以编程方式使用xib设置IBOutlet和文件所有者会导致键值编码兼容错误 - Using xib programatically setting IBOutlet and file owner results in key value coding-compliant error 类不符合键值编码 - Class is not key value coding compliant
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM