Skip to content

Reusable NSTableCellViews #12

@RCCoop

Description

@RCCoop

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's var content: (Data.Element) -> NSView closure with two closures--
    • var generateNewCell: () -> CellType which creates an empty cell
    • var configureCell: (CellType, Data.Element) -> Void to apply the Data.Element to the empty cell.
  • In OutlineViewDelegate.outlineView(_:viewFor:item:):
    • attempts to get a reused CellType cell from the NSOutlineView, and if it fails to get that, it creates a new one with generateNewCell(), and applies a reuseIdentifier in code, which will allow that cell view to be reused later.
    • After getting the CellType view, calls configureCell closure to apply the Data.Element to the view.

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions