NSTableView register cell for reuse

Hi,


Recently, i started to develop an application for OS X with swift 4.2 with a minimal style programming (without nibs and xibs).

Surprisingly, i found that i can't register for reuse a view without a nib file for views in this tableView.

Look at the documentation: https://developer.apple.com/documentation/appkit/nstableview


So I understand that my cells are not reuse and allways create a new instance of this, lowering the performance of the application.


Is there any way to reuse a class like in NSCollectionView?


https://developer.apple.com/documentation/appkit/nscollectionview

Thanks for all.

Accepted Reply

In IOS, you can register the cell class

func register(_ cellClass: AnyClass?, forCellReuseIdentifier identifier: String)


But effectively, seems that does not exist for OSX TableView (contrary to CollectionViews), need to register the nib

func register(_ nib: NSNib?, forIdentifier identifier: NSUserInterfaceItemIdentifier)


What I do usually in OSX Apps is to call

if let cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "CellID"), owner: self)

in

func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {


Not sure it has any noticeable impact on performance.

Replies

In IOS, you can register the cell class

func register(_ cellClass: AnyClass?, forCellReuseIdentifier identifier: String)


But effectively, seems that does not exist for OSX TableView (contrary to CollectionViews), need to register the nib

func register(_ nib: NSNib?, forIdentifier identifier: NSUserInterfaceItemIdentifier)


What I do usually in OSX Apps is to call

if let cellView = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "CellID"), owner: self)

in

func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {


Not sure it has any noticeable impact on performance.