有问题的数据源方法的签名如下:
func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: AnyObject) -> Bool
item
就是您通过-outlineView(_:child:ofItem:)
传递给数据源对象之一,所以搞清楚什么样的对象呢并且使用该对象的属性来计算出你应该返回的内容。在下面我的数据源代码很简单 - 一切都是Document
:
class Document: NSObject {
dynamic var title: String?
dynamic var documents = NSMutableArray() // children
dynamic var isPage: NSNumber! // isExpandable
dynamic weak var parent: Document?
}
这里的数据源:
// MARK:- NSOutlineViewDataSource
// <contents> is the array holding the top-level tree objects
func outlineView(outlineView: NSOutlineView, numberOfChildrenOfItem item: AnyObject?) -> Int {
return item == nil ? contents.count : (item as! Document).documents.count
}
func outlineView(outlineView: NSOutlineView, isItemExpandable item: AnyObject) -> Bool {
return (item as! Document).isPage.boolValue == false
}
func outlineView(outlineView: NSOutlineView, child index: Int, ofItem item: AnyObject?) -> AnyObject {
// The object returned here is always a Document
return item == nil ? contents[index] : (item as! Document).documents[index]
}
我花了一段时间,但可选的链接示例是我的数据模型最好的工作。我认为Swift和Obj-C之间的区别是语法,但是最近几天让我意识到Swift非常强大,需要采用不同的思维方式才能将它用于Cocoa应用程序。太奇妙了。只有一个 ”!”有必要让它工作。谢谢!你们都很棒。 – Charlie