[英]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
有一个连接到IBOutlet
的UICollectionView
。 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编辑:我附上了我的设置截图
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:
有时会发生:
Try cut outlets loose and reconnect them.尝试将插座剪松并重新连接。
Make sure Collection Reusable View identifier is defined in xib file:确保在 xib 文件中定义了集合可重用视图标识符:
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
-自定义类UITableViewCell
到SelectorTableViewCell
和连接SelectorView
里面contentView
到“SelectorTableViewCell”的出口。
You can try the following:您可以尝试以下操作:
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 。
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
删除它们。
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.