user965972
user965972

Reputation: 2597

Programmatically collapse a group row in NSOutlineView

I have an NSOutlineView with an action (see code) that collapse a row when the user clicks anywhere on that row. However it is not working for group.

Some rows are defined as group via the "shouldShowOutlineCellForItem" delegate method.

I can expand a group row programmatically, but not collapse it. Any suggestions? isExpanded is correctly set via the notifications.

@IBAction func didClick(sender: AnyObject?)
{
    assert(self.root != nil)
    let selectedRow = outlineView.clickedRow
    let proposedItem = (selectedRow == -1) ? self.root! : outlineView.itemAtRow(selectedRow) as! thOutlineNode

    if proposedItem.isExpanded
    {
        self.outlineView.collapseItem(proposedItem)
    }
    else
    {
        self.outlineView.expandItem(proposedItem)
    }
}

Upvotes: 0

Views: 514

Answers (1)

dfrib
dfrib

Reputation: 73226

Possibly duplicate. Based on this existing SO question covering Objective-C, try adding the NSOutlineViewDelegate delegate method

func outlineView(_ outlineView: NSOutlineView, shouldShowOutlineCellForItem item: AnyObject) -> Bool {
    return true
}

to the view controller of your NSOutlineView. From the Apple documentation for the NSOutlineViewDelegate, we see that this is expected behaviour:

optional func outlineView(_ outlineView: NSOutlineView, shouldShowOutlineCellForItem item: AnyObject) -> Bool

...

Discussion

Returning NO causes frameOfOutlineCellAtRow: to return NSZeroRect, hiding the cell. In addition, the row will not be collapsible by keyboard shortcuts.

Upvotes: 1

Related Questions