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

时间:2017-11-08 15:43:36

标签: ios swift uiview uicollectionview xib

我有一个带有XIB的自定义UIView。此自定义UIView的{​​{1}}与UICollectionView相关联。在视图设置中,IBOutlet已正确初始化,并且不是零。

但是在UICollectionView方法中,我收到此错误: -

  

由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[setValue:forUndefinedKey:]:此类不是密钥值编码兼容的key selectorCollectionView。'

如果删除数据源并委托,我不会收到任何错误。如果我添加它们,请在此行中输入错误: -

cellForItemAtIndexPath

请帮忙!

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

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

4 个答案:

答案 0 :(得分:2)

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

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

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

  3. 确保在xib文件中定义了collection-view单元格的自定义类:

  4. Set custom class here

    修改 我挖掘你的项目,这是我的发现

    UICollectionView应该是init'在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应该是这样的(删除集合视图init'):

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

    Seleclass SelectorTableViewCell同样。

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

    Main.storyboard - 自定义班级UITableViewCellSelectorTableViewCell,并将SelectorView内的contentView连接到' SelectorTableViewCell'' s出口。

    我认为。 Here是项目:

答案 1 :(得分:0)

在我们的情况下,问题出在Storyboard中的连接错误。单击Storyboard中的自定义类-不是文件所有者-然后将IB插座连接到自定义类中的变量。

答案 2 :(得分:0)

如果尝试注册您的自定义类别和单元格ID

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

答案 3 :(得分:0)

您可以尝试以下操作:

  1. 点击YourCell.xib左侧面板上的文件所有者。然后转到属性检查器,并检查 Class 字段中是否指定了任何类。如果没有-在这里没有我的帮助。如果是,请删除它,然后点击 Enter

  2. YourCell.xib中依次单击所有子视图,然后删除 Connections Inspector 中的所有插座。无需在YourCell.swift中删除它们。

  3. 一旦您单元的文件所有者和插座被移除,我们就可以重新连接插座了。只需像往常一样将每一个拖到YourCell.swift中现有的对应IBOutlet中即可。

因此,当您点击YourCell.xib左侧面板上的 YourCell 并转到 Connections Inspector 时,您会看到所有插座已连接。像这样:

enter image description here

如果您在 YourCell 中检查特定视图的连接,您应该会看到它的插座已连接到 YourCell (我的 StaticSlot 情况)。

enter image description here

这对我有所帮助,希望您能获得同样的体验。