Ios 无法加载捆绑中的NIB。可可荚项目

Ios 无法加载捆绑中的NIB。可可荚项目,ios,swift,cocoapods,bundle,xib,Ios,Swift,Cocoapods,Bundle,Xib,我正在做一个简单的可可豆荚,这是一个自定义UICollectionView。但我无法使用UICollectionViewCell的xib文件。我收到错误:由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法加载捆绑包中的NIB:“NSBundle(loaded)”,名称为“CWNumberedCollectionViewCell” import UIKit class CWNumberedCollectionViewCell: UICo

我正在做一个简单的可可豆荚,这是一个自定义UICollectionView。但我无法使用UICollectionViewCell的xib文件。我收到错误:由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法加载捆绑包中的NIB:“NSBundle(loaded)”,名称为“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")
    }
}

您要查找的模块中没有xib。必须精确确定xib所在的当前模块束,例如使用
let bundle=bundle(for:self)
。否则,链接器将在主捆绑包中查找资源

下面是我如何从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
}