I want to expand the list of sources using NSOutlineView in a Swift project.
The view controller below works well when the delegate method of isGroupItem is not called. However, many __NSMallocBlock__ elements will be returned using the isGroupItem method. Which I have no idea where these items come from. The elements I provided are just strings.
class ViewController: NSViewController, NSOutlineViewDataSource, NSOutlineViewDelegate { let topLevel = ["1", "2"] let secLevel = ["1": ["1.1", "1.2"], "2": ["2.1", "2.2"]] func outlineView(outlineView: NSOutlineView, numberOfChildrenOfItem item: AnyObject?) -> Int { if let str = item as? String { let arr = secLevel[str]! as [String] return arr.count } else { return topLevel.count } } func outlineView(outlineView: NSOutlineView, isItemExpandable item: AnyObject) -> Bool { return outlineView.parentForItem(item) == nil } func outlineView(outlineView: NSOutlineView, child index: Int, ofItem item: AnyObject?) -> AnyObject { var output: String! if let str = item as? String { output = secLevel[str]![index] } else { output = topLevel[index] } return NSString(string: output) } func outlineView(outlineView: NSOutlineView, objectValueForTableColumn tableColumn: NSTableColumn?, byItem item: AnyObject?) -> AnyObject? { return item } func outlineView(outlineView: NSOutlineView, isGroupItem item: AnyObject) -> Bool { return (outlineView.parentForItem(item) == nil) } func outlineView(outlineView: NSOutlineView, viewForTableColumn tableColumn: NSTableColumn?, item: AnyObject) -> NSView? { return outlineView.makeViewWithIdentifier("HeaderCell", owner: self) as NSTextField } }
A sample project can be downloaded here.
cocoa swift nsoutlineview
Kelvin
source share