我尝试遵循本指南:
Quick Start for Collection Views
在 Collection View 项中使用 NSImageView。
什么也没有显示,无论我用图像设置图像还是通过代码设置数组。
所以我尝试使用
以编程方式完成此操作func representedObject(representedObject: AnyObject)
{
super.representedObject = representedObject
photoImageView.image = (representedObject as! NSImage)
println("\(representedObject)")
}
在 Collection View 项(子类)中。
如果我不子类化 Collection View Item Xcode 会告诉我没有原型(prototype)集,如果我子类化它它会告诉我“无法加载 nibName”...(它在 Storyboard中具有正确的标识集)
我无法让这个 Collection View 工作:-(
无论如何,我喜欢绑定(bind)...所以我想通过绑定(bind)获得正确的结果.. 我检查并重新检查了链接中文档中的每个段落,一切似乎都很好。主要区别在于文档使用应用程序委托(delegate),而我使用的是 View Controller 。
我用 swift 翻译了 KVC 方法,我认为它们是正确的,因为我知道它们已被调用。它们是:
func insertObject(p: ClientPhoto, inClientPhotoArrayAtIndex index: Int) {
images.insertObject(p, atIndex: index)
}
func removeObjectFromClientPhotoArrayAtIndex(index: Int) {
images.removeObjectAtIndex(index)
}
func setClientPhotoArray(a: NSMutableArray) {
images = a
}
func clientPhotoArray() -> NSArray {
return images
}
最佳答案
基本上有两种使用 NSCollectionView 的方法。一种是设置 itemPrototype
属性,另一种是重写 newItemForRepresentedObject
。覆盖方法更灵活,优点是您可以使用下面的技术在 Storyboard中创建 nscollectionviewitem,并且所有导出都将被正确设置。以下是我如何使用它的示例:
class TagsCollectionView: NSCollectionView {
// ...
override func newItemForRepresentedObject(object: AnyObject!) -> NSCollectionViewItem! {
let viewItem = MainStoryboard.instantiateControllerWithIdentifier("tagCollectionViewItem") as! TagCollectionViewItem
viewItem.representedObject = object
return viewItem
}
关于cocoa - NSCollectionView 不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30689194/