2016-06-01 8 views
1

「ImageCollectionViewItem」と呼ばれるアイテムを表示するNSCollectionViewがあります。アイテムをカバーするNSViewだけを持つImageCollectionViewItem.xibファイルがあります。私は、そのビューにあるものを動的に変更できるようにしたいが、参照しようとすると、それはゼロになる。私はすでに次のような質問にチェック:outlets in UIViewController nil in viewdidloadを、それは私の問題ではありません、私はそれをリンクされて、私はここで黒丸NSCollectionViewItemにはitemForRepresentedObjectAtIndexPathにnilコンセントがありません

を持っているのViewController拡張です:

extension ViewController : NSCollectionViewDataSource { 

    func collectionView(collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int { 
     return titles.count 
    } 
    func collectionView(collectionView: NSCollectionView, itemForRepresentedObjectAtIndexPath indexPath: NSIndexPath) -> NSCollectionViewItem { 
     let item = collectionView.makeItemWithIdentifier("ImageCollectionViewItem", forIndexPath: indexPath) as! ImageCollectionViewItem 

     //This is what is nil: 
     let thisView = item.iconView 

     return item 
    } 

} 

そして、ここではImageViewCollectionItemコードは

です
class ImageCollectionViewItem: NSCollectionViewItem { 

    @IBOutlet weak var iconView: NSView! 

    var numItem: Int? 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do view setup here. 

     view.wantsLayer = true 
     view.layer?.backgroundColor = NSColor.whiteColor().CGColor 
    } 
} 

答えて

4

これは、ココアの世界で確かに奇妙なものです。しかし、ここに役立ついくつかの流れがあります。 Interface BuilderでImageCollectionViewItem.xibを開き、オブジェクトコレクションから「コレクションビューアイテム」をドラッグして選択し、カスタムクラス名をImageCollectionViewItemに設定して、makeItemWithIdentifierが非nilアイテムを作成するようにします。私の例では

私はタイルのクラスを持っている:

class Tile: NSCollectionViewItem { 

そして、ここでは、私はタイルを作成する方法である:ここでは

func collectionView(collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int { 

    return items.count 
} 


func collectionView(collectionView: NSCollectionView, 
        itemForRepresentedObjectAtIndexPath indexPath: NSIndexPath) -> NSCollectionViewItem { 

    let item = collectionView.makeItemWithIdentifier("Tile", forIndexPath: indexPath) as! Tile 
    return item 


} 

は、IBの写真です:

enter image description here

これらの手順を実行すると、アウトレットが作成時に接続されます。私の場合は2店舗あります

class Tile: NSCollectionViewItem { 

@IBOutlet weak var bigScrollView: ItemScrollView! 
@IBOutlet weak var smallScrollView: ItemScrollView! 

override func awakeFromNib() { 
    super.awakeFromNib() 
} 

override func viewDidAppear() { 
    // 
} 

} 

をそして、あなたが絵に見ることができるように、彼らは両方とも接続して取得されています

enter image description here

+0

私は、しかし、同じ問題を持っているし、あなたのソリューションユージンを試してみました私はまだ私のimageViewのためのnilを取得します。 myImageViewという新しいコンセントを作成しようとしましたが、それもnilを返します。 –

+0

サンプルプロジェクトを共有できますか?私はそれが少し見落とされていると確信しています。時には私にも起こります。 –

関連する問題