带有 UICollectionView 的 xib - 不符合键值编码
xib with UICollectionView - not key value coding compliant
我有一个带有 XIB 的自定义 UIView
。这个习俗 UIView
有一个 UICollectionView
连接到 IBOutlet
。在视图设置中,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.'
如果我删除数据源和委托,我不会收到任何错误。如果我添加它们,我会在这一行出现错误:-
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "selectorCell", for: indexPath) as! SelectorCell
请帮忙!
编辑:我附上了我的设置截图
我也在这里上传了项目http://www.fast-files.com/getfile.aspx?file=148623
可能是同步问题。有时会发生:
尝试松开插座并重新连接。
确保在 xib 文件中定义了集合可重用视图标识符:
确保集合视图单元格的自定义 class 在 xib 文件中定义:
编辑:
我深入研究了您的项目,这是我的发现
UICollectionView 应该是 awakeFromNib
方法中的 init'(覆盖它):
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 应该看起来像这样(删除集合视图初始化'):
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 同样。
class SelectorTableViewCell: UITableViewCell {
@IBOutlet weak var selectorView: SelectorView!
}
In Main.storyboard
- 自定义 class UITableViewCell
到 SelectorTableViewCell
并将 SelectorView
in contentView
连接到 'SelectorTableViewCell''s出口。
我想就是这样。 Here 是项目:
在我们的例子中,问题是在 Storyboard 中建立了错误的连接。单击情节提要中的自定义 class——而不是文件所有者——然后将 IB 插座连接到自定义 class 中的变量。
如果尝试注册您的自定义 class 和小区 ID
self.collectionView.register(nib, forCellWithReuseIdentifier: "Cell")
self.collectionView.register(CustomClassCell.self, forCellWithReuseIdentifier: "Cell")
您可以尝试以下方法:
单击YourCell.xib
左侧面板中的文件所有者。然后到Attributes inspector检查Class字段中是否有指定的class。如果没有 - 我在这里没有帮助。如果是 - 将其删除并点击 Enter.
在YourCell.xib
中依次点击所有子视图并删除Connections Inspector中的所有出口。无需在 YourCell.swift
.
中删除它们
一旦您的手机的文件所有者和插座被删除,我们就可以重新连接插座了。只需像往常一样将每个拖到 YourCell.swift
.
中现有的相应 IBOutlet
因此,当您单击 YourCell.xib
左侧面板中的 YourCell 并转到 Connections Inspector 时,您会看到所有插座都已连接。像这样:
并且如果您检查 YourCell 中特定视图的连接,您应该会看到它的出口连接到 YourCell(StaticSlot 在我的例子中)。
这对我有帮助,希望你也能得到同样的帮助。
我有一个带有 XIB 的自定义 UIView
。这个习俗 UIView
有一个 UICollectionView
连接到 IBOutlet
。在视图设置中,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.'
如果我删除数据源和委托,我不会收到任何错误。如果我添加它们,我会在这一行出现错误:-
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "selectorCell", for: indexPath) as! SelectorCell
请帮忙!
编辑:我附上了我的设置截图
我也在这里上传了项目http://www.fast-files.com/getfile.aspx?file=148623
可能是同步问题。有时会发生:
尝试松开插座并重新连接。
确保在 xib 文件中定义了集合可重用视图标识符:
确保集合视图单元格的自定义 class 在 xib 文件中定义:
编辑: 我深入研究了您的项目,这是我的发现
UICollectionView 应该是 awakeFromNib
方法中的 init'(覆盖它):
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 应该看起来像这样(删除集合视图初始化'):
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 同样。
class SelectorTableViewCell: UITableViewCell {
@IBOutlet weak var selectorView: SelectorView!
}
In Main.storyboard
- 自定义 class UITableViewCell
到 SelectorTableViewCell
并将 SelectorView
in contentView
连接到 'SelectorTableViewCell''s出口。
我想就是这样。 Here 是项目:
在我们的例子中,问题是在 Storyboard 中建立了错误的连接。单击情节提要中的自定义 class——而不是文件所有者——然后将 IB 插座连接到自定义 class 中的变量。
如果尝试注册您的自定义 class 和小区 ID
self.collectionView.register(nib, forCellWithReuseIdentifier: "Cell")
self.collectionView.register(CustomClassCell.self, forCellWithReuseIdentifier: "Cell")
您可以尝试以下方法:
单击
YourCell.xib
左侧面板中的文件所有者。然后到Attributes inspector检查Class字段中是否有指定的class。如果没有 - 我在这里没有帮助。如果是 - 将其删除并点击 Enter.在
中删除它们YourCell.xib
中依次点击所有子视图并删除Connections Inspector中的所有出口。无需在YourCell.swift
.一旦您的手机的文件所有者和插座被删除,我们就可以重新连接插座了。只需像往常一样将每个拖到
中现有的相应 IBOutletYourCell.swift
.
因此,当您单击 YourCell.xib
左侧面板中的 YourCell 并转到 Connections Inspector 时,您会看到所有插座都已连接。像这样:
并且如果您检查 YourCell 中特定视图的连接,您应该会看到它的出口连接到 YourCell(StaticSlot 在我的例子中)。
这对我有帮助,希望你也能得到同样的帮助。