Search code examples
macosnstableviewswiftuiappkit

How to build an AppKit table view using SwiftUI?


We have got a lot of tutorials for iOS with code like this:

List(items) { item in
  NavigationLink(destination: ItemView(item: item)) {
    ItemRow(item: item)
  }
}

On macOS however, this code results in a list of disabled table cell views.

So how do we build something like NSTableView using SwiftUI?


Solution

  • In order to use NavigationLink, you’ll need to be in a NavigationView context. Wrapping your current view hierarchy in a NavigationView should fix your issue.

    NavigationView {
        List(items) { item in
          NavigationLink(destination: ItemView(item: item)) {
            ItemRow(item: item)
          }
        } 
    }