2017-10-03 7 views
1

私はカスタムUICollectionViewである単純なココアポッドをやっています。しかし、私はUICollectionViewCellのxibファイルを使用することができません。私はエラーを取得しています:未知の例外 'NSInternalInconsistencyException'の理由でアプリケーションを終了しています:理由: 'バンドルでNIBをロードできませんでした:' NSBundle(ロード) '名前' CWNumberedCollectionViewCell '。NIBをバンドルにロードできませんでした。 Cocoa Podプロジェクト

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") 
    } 
} 

I have added the xib file to copy bundle resource.

答えて

1

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 
} 
関連する問題