DEV Community

Cover image for Adding an NSTableView to a SwiftUI View

Adding an NSTableView to a SwiftUI View

Hugh Jeremy on March 18, 2020

SwiftUI made a surprisingly good first impression. Of course, things get more complicated as soon as you want to build something actually useful. T...
Collapse
 
hugh_jeremy profile image
Hugh Jeremy

I received a follow up question, asking how I added the unified NSToolbar visible in the app screenshot. Walkthrough here: dev.to/hugh_jeremy/adding-a-unifie...

Collapse
 
markknopper profile image
Mark Knopper

Hi, thanks for this tutorial about NSTableView in SwiftUI. Would it be possible for you to upload your project that does this successfully? I keep running into errors and crashes on initializing from NSViewControllerRepresentable and makeNSViewController. I am currently getting this when returning from makeNSViewController:

-[NSNib _initWithNibNamed:bundle:options:] could not load the nibName: EntityNSTableViewController in bundle (null).

followed by a EXC_BAD_ACCESS crash when the window is brought to the front.

Collapse
 
hugh_jeremy profile image
Hugh Jeremy

Hi Mark, sorry for my slow reply! I have been doing some more work with NSTableView in SwiftUI recently, so it's possible I could smash something together to upload... I have a few macOS apps in the pipe that I might be able to open source. I'll try to do so soon!

Collapse
 
gabeklavans profile image
Gabriel Klavans

I'd also love an example project! I'm learning to build a MacOS app and it's tough to learn aspects of NSTableView concurrently with SwiftUI concepts...

Thread Thread
 
hugh_jeremy profile image
Hugh Jeremy

Yeah NSTableView definitely requires a totally different mindset than SwiftUI. I'd recommend building some stuff in AppKit to get used to the "old" way of doing things. It's so common to need to drop back down into AppKit, and NSViewRepresentable becomes very important. Another example is AuthenticationServices - To add a "Sign in with Apple" button you're back in AppKit land writing @objc interface implementations!

Collapse
 
shs_hassan profile image
Hassan Altaf

I wanted to understand how .refresh() method works? Do you need to add the functionality in the class PlayerNSTableController: NSViewController {} or is this an automatic thing?

Also maybe this is irrelevant but if i want to do the same in ios app, i can use table view from uikit. My main worry is how i can make additional functionality on the table for example if i want to highlight one of the rows in the table. How can i accomplish that in swiftui interfacing to appkit or uikit. Do you know?

Collapse
 
hugh_jeremy profile image
Hugh Jeremy

Hi Hassan! I've never done any work in UIKit, so I can't speak for how similar the UIKit TableView is to the AppKit NSTableView. My impression is that generally UIKit and AppKit classes have only superficial similarities.

As for additional functionality - Honestly, I've found that NSTableView is great for simple tables, but can get fiddly if you try to skew it too far beyond Apple's vision for what constitutes a "table". The key is to use NSView as the basis for your cell views - Get comfortable with drawing custom NSViews, rather than trying to tweak something like an NSButton to suit what you're trying to do.

Overall, I would say the best option is to ditch AppKit and UIKit and work with SwiftUI. It's far easier to create fully custom tables as SwiftUI views, in my opinion. Here's some first impressions I had of SwiftUI: dev.to/hugh_jeremy/swiftui-first-i...

Collapse
 
voidgraphics profile image
Adrien Leloup

Thanks for the tutorial - but is it really far easier to create a fully custom table view in SwiftUI? How do you handle the column auto sizing and ability to manually resize them?

Collapse
 
dus2thao profile image
hqt

This tutorial is a hoax. Even the class PlayerNSTableView found nowhere in anywhere else.

Collapse
 
kdeda profile image
kdeda

Thanks for the write up.
These tips allow us with good existing code to upgrade to the modern world of swift ui.

Collapse
 
clemens05 profile image
Clemens R

Hello,

I work with SwiftUI 2 (and I am a beginner) and I don't know how to add the module DraftSport and I don't know what DraftSport is...