UICollectionView,来自注册为单元格的框架目标的nib在运行时失败

ale*_*ent 7 xcode ios ios-frameworks uicollectionview swift

我有一个在xib中绘制的UICollectionViewCell,并伴有swift类文件.这两个文件是我的应用程序的UI框架目标(称为SomeUI)的一部分.

在我的集合视图的视图控制器中(在app目标中),我将nib注册为集合视图单元格.我的视图控制器代码如下;

import UIKit
import SomeUI

let reuseIdentifier = "Cell"

class ACollectionViewController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView.registerNib(UINib(nibName: "JamesTheCollectionViewCell", bundle: NSBundle(identifier: "SomeUI")), forCellWithReuseIdentifier: reuseIdentifier)
    }

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView!) -> Int {
        return 1
    }

    override func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int {
        return 1
    }

    override func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as JamesTheCollectionViewCell
        cell.titleLabel.text = "yippee"
        return cell
    }
}
Run Code Online (Sandbox Code Playgroud)

在nib中,单元格视图已正确设置其自定义类; JamesTheCollectionViewCell在模块SomeUI中.

xcode中的单元xib的screengrab

虽然Xcode似乎对这个设置很满意,但是在运行时我得到一个错误,因为找不到JamesTheCollectionView.swift中定义的类.

Interface Builder文件中的未知类_TtC19collectionViewThing26JamesTheCollectionViewCell.

如果我将JamesTheCollectionView.swift的目标成员资格从框架切换到应用程序(但将xib作为框架的一部分),一切运行正常.但是,我要求xib文件从框架加载类...我错过了什么,为什么当单元格实现类是框架的一部分时它不起作用?

作为参考,这是单元实现文件:

import UIKit

public class JamesTheCollectionViewCell: UICollectionViewCell {
    @IBOutlet public weak var titleLabel: UILabel!
}
Run Code Online (Sandbox Code Playgroud)

ale*_*ent 7

解决了.

TL:DR; 更改文件的目标成员资格时,请记住在下次构建之前执行产品>清理!

说明:问题是"SomeUI"不是有效的UIBundle标识符,因此NSBundle(identifier: "SomeUI")返回nil,因此registerNib行从应用程序包而不是框架注册了一个nib.应用程序包不包含单元格实现类,因此崩溃了.

但是......应用程序包也不应该包含笔尖?那么那里发生了什么?我一直在为nib和实现文件切换目标成员资格.我没有在构建之间运行'Clean',因此nib的副本留在了应用程序目标构建文件夹中.

到原来的问题的解决方案是通过替换正确加载从SomeUI框架笔尖NSBundle(identifier: "SomeUI")NSBundle(forClass: JamesTheCollectionViewCell.self)