UICollectionView,来自注册为单元格的框架目标的 nib 在运行时失败 [英] UICollectionView, nib from a framework target registered as a cell fails at runtime
问题描述
我在 xib 中绘制了一个 UICollectionViewCell,并附有 swift 类文件.这两个文件是我应用的 UI 框架目标(称为 SomeUI)的一部分.
I have a UICollectionViewCell drawn out in a xib and accompanied by swift class file. These two files are part of my app's UI framework target (called SomeUI).
在我的集合视图的视图控制器中(在应用程序目标中),我将笔尖注册为集合视图单元格.我的视图控制器代码如下;
In the view controller of my collection view (in the app target) I register the nib as a collection view cell. My view controller code is as follows;
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
}
}
在 nib 中,单元格视图已适当设置了其自定义类;模块 SomeUI 中的 JamesTheCollectionViewCell.
In the nib, the cell view has its custom class set appropriately; JamesTheCollectionViewCell in module SomeUI.
虽然 Xcode 似乎对这个设置很满意,但在运行时我收到一个错误,因为找不到 JamesTheCollectionView.swift 中定义的类.
While Xcode seems quite happy with this setup, at runtime i get an error as the class defined in JamesTheCollectionView.swift cannot be found.
未知类 _TtC19collectionViewThing26JamesTheCollectionViewCell 中界面生成器文件.
Unknown class _TtC19collectionViewThing26JamesTheCollectionViewCell in Interface Builder file.
如果我将 JamesTheCollectionView.swift 的目标成员资格从框架切换到应用程序(但将 xib 作为框架的一部分),一切运行正常.但是,我要求 xib 文件从框架加载类……我错过了什么,为什么当单元实现类是框架的一部分时它不起作用?
If I switch the target membership for JamesTheCollectionView.swift from the framework to the application (but leave the xib as part of the framework), everything runs fine. But, I asked the xib file to load the class from the framework… what am i missing, why doesn't it work when the cell implementation class is part of the framework?
作为参考,这里是单元实现文件:
For reference, here is the cell implementation file:
import UIKit
public class JamesTheCollectionViewCell: UICollectionViewCell {
@IBOutlet public weak var titleLabel: UILabel!
}
推荐答案
已解决.
TL:DR;更改文件的目标成员资格时,请记住在下次构建之前执行 Product > Clean!
TL:DR; When changing target membership for files, remember to do Product > Clean before next build!
说明:问题是SomeUI"不是有效的 UIBundle 标识符,因此 NSBundle(identifier: "SomeUI")
返回 nil,因此 registerNib 行从应用程序包而不是框架中注册了一个笔尖.应用程序包不包含单元实现类,所以它崩溃了.
Explanation:
The problem was that "SomeUI" isn't a valid UIBundle identifier and so NSBundle(identifier: "SomeUI")
returned nil, so the registerNib line registered a nib from the application bundle not the framework. The application bundle didn't contain the cell implementation class so it crashed.
但是……应用程序包也不应该包含笔尖?那么那里发生了什么?我一直在切换 nib 和实现文件的目标成员资格.我没有在构建之间运行Clean",因此在应用程序目标构建文件夹中保留了笔尖的副本.
But… the application bundle shouldn't have contained the nib either? So what was going on there? I had been switching the target membership for the nib and implementation file. I had not been running 'Clean' between builds, so a copy of the nib was left in the application target build folder.
原始问题的解决方案是通过将 NSBundle(identifier: "SomeUI")
替换为 NSBundle(forClass: JamesTheCollectionViewCell.self)
The solution to original problem was to correctly load the nib from the SomeUI framework by replacing NSBundle(identifier: "SomeUI")
with NSBundle(forClass: JamesTheCollectionViewCell.self)
这篇关于UICollectionView,来自注册为单元格的框架目标的 nib 在运行时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!