Cocoa - Where is the link between a NSCollectionView and a NSCollectionViewItem? Xcode 6 Bug?

淺唱寂寞╮ 提交于 2019-11-29 21:46:53
mostruash

Well, I gave @RubberDuck's workaround a go but it didn't work (see my comment). What worked for me is setting collectionView.itemPrototype in viewDidLoad of the view controller (Xcode 6.1):

@IBOutlet weak var collectionView: NSCollectionView!

override func viewDidLoad() {
    // don't forget to set identifier of CollectionViewItem 
    // from interface builder
    let itemPrototype = self.storyboard?.instantiateControllerWithIdentifier("collectionViewItem")
        as NSCollectionViewItem
    self.collectionView.itemPrototype = itemPrototype
}

Yes, after hours struggling I confirm this one more of Xcode bugs.

The only solution is to edit the file Main.storyboard and add this line to the end of CollectionView section, just before </collectionView>:

<connections>
  <outlet property="itemPrototype" destination="XXXXXXX" id="Kaa-2J-b4e"/>
</connections>

where XXXXXX is the id or the CollectionViewItem. The other number and you can keep the one I post, unless this id is already used by your project, but the chances of this happens is very dim.

I couldn't get the accepted answer from @mostruash to work in Objective-C either, but I came up with another workaround. Use a custom setter for the collectionView IBOutlet property:

- (void)setCollectionView:(NSCollectionView *)collectionView {
    NSCollectionViewItem *itemPrototype = [self.storyboard instantiateControllerWithIdentifier:@"collection_item"];
    collectionView.itemPrototype = itemPrototype;
    _collectionView = collectionView;
}

Selected answer needs more information:

If you make binding from collectionView's content to array controllers arranged objects using Interface Builder's bindings, it will throw error: "NSCollectionView item prototype must not be nil".

This is because bindings are made before itemPrototype is assigned. Instead, make bindings with code after itemPrototype assignment.

@IBOutlet weak var collectionView: NSCollectionView!
@IBOutlet var arrayController: NSArrayController!
let itemPrototype = self.storyboard?.instantiateControllerWithIdentifier("collectionViewItem")
        as NSCollectionViewItem
self.collectionView.itemPrototype = itemPrototype
collectionView.bind("content", toObject: arrayController, withKeyPath: "arrangedObjects", options: nil)
ingconti

I fall in the same probleam, anyway since 10.11 Apple Documents says:

" You must also disconnect and discard the NSCollectionView’s “itemPrototype”, which is a vestige of the 10.10-and-earlier API model."

from: https://developer.apple.com/library/mac/releasenotes/AppKit/RN-AppKit/#10_11CollectionView

here is explained how it works now.

So we must use old beloved XIBS:

NSCollectionViewItem *item = [collectionView makeItemWithIdentifier:@"Thing" forIndexPath:indexPath];

The NSCollectionView has an outlet itemPrototype that's connected to its specific NSCollectionViewItem. You can see this in the Connections inspector for either object.

Go to the Object Library (the bottom right of Xcode) and search for "Object". It "Provides an instance of an NSObject subclass that is not available in Interface Builder." Drag and drop that into the storyboard, onto the top bar of the ViewController (or alternatively under the ViewController in the tree-view on the LHS.) Then, click on that Object, and in the "Identity Inspector" set its class to be your ViewItem class. Then you can then ctrl+drag from the Collection View to the Object, and it will let you wire it up as "itemPrototype".

Guys the trick seem to be that you cannot use a regular ViewController from the Object collection in Xcode when using Storyboards.

What works for me is dragging a collection view item from the Objects list and setting its class to your custom class then wiring up all the connections as you normally would to create IBOutlets.

Then you have to instantiate the view instead of makeView but this way allows you to use multiple views in the same collection without registering anything special.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!