NSCollectionViewItem永不实例化 [英] NSCollectionViewItem never instantiate

查看:116
本文介绍了NSCollectionViewItem永不实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有点迷路: 我创建了一个类似于colorPicker的按钮:单击该按钮将在弹出框中显示collectionView. 我首先使用包含视图和collectionView(嵌入为scrollView + clipView)的笔尖fil进行了此操作. 这些东西工作正常.

I'm a bit lost here: I created a button acting like a colorPicker: clicking on it shows a collectionView in a popover. I first did it with a nib fil containing a view + the collectionView (embedded in as scrollView + a clipView). The stuff works just fine.

由于nib文件非常简单(并且为了提高我在以编程方式设计视图时的编码技能),我决定摆脱nib文件,并将缺少的部分写入代码中. 问题是,除了collectionView的内容之外,我设法完成了工作.经过深入研究,似乎在方法内部:

As the nib file is very simple (and to improve my coding skills in designing views programmatically), I decided to get rid of the nib file and write the missing part in code. The thing is, I manage to get the job done except for the content of the collectionView. After deep investigation, it appears that, inside the method:

func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem

应该用于管理数据源的方法

which is supposed to manage the data source, the method

collectionView.makeItem(withIdentifier: String, for: IndexPath)

不起作用.实际上,在:

doesn't work. In fact, in:

let item = collectionView.makeItem(withIdentifier: ColorPickerPopover.itemIdentifier, for: indexPath)

正如调试程序在我进入时所说的那样,

项目尚未初始化(不是nil,未初始化).显然,makeItem方法从不实例化我创建的子类中的任何collectionViewItem. 标识符很好,并且就像在nib版本中一样,调用collectionView.register函数,因为这两个项目在这些方面是相同的. makeItem函数根本不会调用我子类化的NSCollectionViewItem的loadView方法.

item is uninitialized, as the debugger says when I step in (not nil, uninitialized). Apparently, the makeItem method never instantiate any collectionViewItem from the subclass I've made. The identifier is fine and the collectionView.register function is called, just like in the nib version, as both projects are identical in these points. The makeItem function simply doesn't call the loadView method of the NSCollectionViewItem I've subclassed.

有任何线索吗?

乔什

推荐答案

使用collectionView.makeItem(withIdentifier:for:)方法,您首先需要在集合视图中注册类或nib文件:

With the collectionView.makeItem(withIdentifier:for:) method, you'll first need to either register the class or the nib file with the collection view:

使用课程

使用register(_:forItemWithIdentifier:)(第一个参数接受AnyClass?)

Use register(_:forItemWithIdentifier:) (the first parameter accepts AnyClass?)

collectionView.register(MyCustomCollectionViewItemSubclass.self, forItemWithIdentifier: "SomeId")

使用Nib文件

使用register(_:forItemWithIdentifier:)(第一个参数接受NSNib?).

Use register(_:forItemWithIdentifier:) (the first parameter accepts NSNib?).

let nib = NSNib(nibNamed: "MyCollectionViewItem", bundle: nil)!
collectionView.register(nib, forItemWithIdentifier: "SomeId")

关键:在Nib文件中,还必须确保NSCollectionViewItem添加到场景中.您还必须将对象的类设置为子类以使其起作用(可以在检查器的面板上进行设置).

The key thing: On your Nib file, you also have to make sure that you have an NSCollectionViewItem added to the scene. You also have to set the object's class to your subclass in order for it to work (you can set it on the inspector's panel).

希望这会有所帮助!

这篇关于NSCollectionViewItem永不实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆