-
Notifications
You must be signed in to change notification settings - Fork 9
Description
NSOutlineViewDelegate.outlineView(_:viewFor:item:) discussion says that the view creation should use reusable cell views:
It is recommended that the implementation of this method first call the NSTableView method makeView(withIdentifier:owner:) passing, respectively, the tableColumn parameter’s identifier and self as the owner to attempt to reuse a view that is no longer visible
The current implementation of OutlineView creates a new NSView for every row and ignores the reusable views aspect of NSOutlineView. This makes implementation simpler (and more SwiftUI-like), but would it be worth adding functionality for reusing views?
I've made a basic implementation of this that increases the complexity of the OutlineView initializers, but maybe there's a better way. Here's how my first-attempt changes the initializer and data-source behavior:
- Adds a new generic type to
OutlineView:CellType: NSView - Replaces
OutlineView'svar content: (Data.Element) -> NSViewclosure with two closures--var generateNewCell: () -> CellTypewhich creates an empty cellvar configureCell: (CellType, Data.Element) -> Voidto apply theData.Elementto the empty cell.
- In
OutlineViewDelegate.outlineView(_:viewFor:item:):- attempts to get a reused
CellTypecell from theNSOutlineView, and if it fails to get that, it creates a new one withgenerateNewCell(), and applies areuseIdentifierin code, which will allow that cell view to be reused later. - After getting the
CellTypeview, callsconfigureCellclosure to apply theData.Elementto the view.
- attempts to get a reused
The main downsides to this approach are that we now have an extra generic type in OutlineView, and more parameters for all of its initializers. The upside is that it follows NSOutlineViewDelegate's preference for reusable views, which I guess is more efficient.