CodexBloom - Programming Q&A Platform

NSOutlineView Not Expanding Correctly on macOS 14 - Issues with DataSource

๐Ÿ‘€ Views: 108 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-09-01
macos nsoutlineview swift Swift

I'm experiencing an issue with an `NSOutlineView` in my macOS 14 application where the outline view does not expand when the user clicks on a parent item. The data source methods are all implemented, but for some reason, the `outlineView(_:childrenOfItem:)` method isn't returning the expected children in some cases. I've set up my data structure as a nested array, and I can successfully retrieve the children programmatically, but the UI doesn't reflect this. Hereโ€™s the relevant part of my implementation: ```swift class MyOutlineViewDataSource: NSObject, NSOutlineViewDataSource { var items: [MyItem] // MyItem has a property children: [MyItem] func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int { guard let parentItem = item as? MyItem else { return items.count } return parentItem.children.count } func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any { let parentItem = item as? MyItem ?? items[index] return parentItem.children[index] } func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool { if let item = item as? MyItem { return !item.children.isEmpty } return true } } ``` I also set the delegate and data source correctly in viewDidLoad: ```swift override func viewDidLoad() { super.viewDidLoad() outlineView.dataSource = dataSource outlineView.delegate = self } ``` Despite this, when clicking on a parent item, no action seems to occur. The `outlineView(_:shouldExpandItem:)` delegate method is not being called at all. Iโ€™ve tried reloading the outline view after updating the data source but it still doesnโ€™t work. No errors are thrown, and Iโ€™ve verified that the `numberOfChildrenOfItem` method is getting called and returns the correct count. What could be causing the outline view to fail to expand? Is there a specific configuration or a known issue with `NSOutlineView` in macOS 14 that I am overlooking? For context: I'm using Swift on Ubuntu. What's the best practice here?