我正在做一个简单的可可pod,它是一个自定义的UICollectionView。但是我无法使用UICollectionViewCell的xib文件。我收到错误:由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:'无法在捆绑中加载NIB:' NSBundle(已加载)'名称' CWNumberedCollectionViewCell'。
import UIKit
class CWNumberedCollectionViewCell: UICollectionViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}
import Foundation
import UIKit
public class CWCollectionViewNumbered: UICollectionView{
let cellIdentifier = "CWCell"
public init(frame: CGRect, layout: UICollectionViewFlowLayout, parent: UIView) {
super.init(frame: frame, collectionViewLayout: layout)
self.allowsMultipleSelection = true
self.delegate = self
self.dataSource = self
self.register( UINib.init( nibName:"CWNumberedCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: cellIdentifier)
self.backgroundColor = UIColor.blue
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override public func selectItem(at indexPath: IndexPath?, animated: Bool, scrollPosition: UICollectionViewScrollPosition) {
print("tapped")
}
}
答案 0 :(得分:1)
xib不在您要查找的模块中。您必须精确xib所在的当前模块包,例如使用let bundle = Bundle(for: self)
。否则,链接器将在主bundle中查找资源。
以下是我从xib文件加载图片的示例:
class func loadImage(imageSize: ImageSize) throws -> UIImage {
let bundle = Bundle(for: self)
let imageName = self.imageName(imageSize)
guard let image = UIImage(named: imageName, in: bundle, compatibleWith: .none) else {
throw LoadingIndicatorError.missingImage
}
return image
}