问题
I have a NSOutlineView, and clicking on a row will expand/collapse the item if it's expandable.
if ([self.outlineView isItemExpanded:item]) {
NSLog("Will collapse item : %@", item);
[[self.outlineView animator] collapseItem:item];
}
else {
[[self.outlineView animator] expandItem:item];
}
Expanding the item works as expected, however collapsing the item is not working. I did get the log before executing collapseItem:, and the item is correct. The delegate method - (BOOL)outlineView:(NSOutlineView *)outlineView shouldCollapseItem:(id)item
was not called too.
Have been on this problem for hours. Any ideas what causes this?
回答1:
I figured it out. Seems the item is collapsable only when - (BOOL)outlineView:(NSOutlineView *)outlineView shouldShowOutlineCellForItem:(id)item
returns YES for that item. Otherwise, you can only expand the item.
回答2:
In my case, my item is struct so those function didn't work. Then I need to change to class, and it worked. Because when I use this code "let item = outlineView.item(atRow: selectedIndex)" in struct it will copy value and create new object. So outlineView can't recognize the item => it won't work
let selectedIndex = outlineView.selectedRow
if let item = outlineView.item(atRow: selectedIndex) {
if outlineView.isItemExpanded(item) {
outlineView.collapseItem(item, collapseChildren: true)
}else {
outlineView.expandItem(item, expandChildren: true)
}
}
回答3:
This works really fine for me
id item = [sender itemAtRow:[sender clickedRow]];
if ([self outlineView:sender isItemExpandable:item]) {
if ([sender isItemExpanded:item])
[sender collapseItem:item];
else
[sender expandItem:item];
}
回答4:
Item of NSOutlineView will collapse or not depends upon below method, return true if you want to collapse otherwise false.
func outlineView(_ outlineView: NSOutlineView, shouldShowOutlineCellForItem item: Any) -> Bool
I tried to make same a demo like MAC system preference -> keyboard -> Shortcuts enter image description here
func outlineView(_ outlineView: NSOutlineView, shouldShowOutlineCellForItem item: Any) -> Bool {
let isExpanded = outlineView.isItemExpanded(item)
if isExpanded {
return true
} else {
return false
}
}
func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
let view = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "OutlineTableCell"), owner: self) as! OutlineTableCell
view.setOutlineTableCellLaytout(forNode: (item as? SectionTreeNode)!)
view.disclosure.action = nil
view.disclosure.action = #selector(didDisclosureClicked(_:))
view.selectedItem = item as? SectionTreeNode
return view
}
@objc func didDisclosureClicked(_ sender: NSButton) {
let view = sender.superview?.superview as? OutlineTableCell
let isExpand = outlineView.isItemExpanded(view?.selectedItem)
isExpand ? outlineView.collapseItem(view?.selectedItem, collapseChildren: true) : outlineView.expandItem(view?.selectedItem, expandChildren: true)
}
My output is below image: enter image description here
来源:https://stackoverflow.com/questions/17181792/nsoutlineview-doesnt-collapse-item