SwiftUi macOs: How to use a row in a Table() as a NavigationLink()

  Kiến thức lập trình

I’m trying to build an App with two rows NavigationSplitView. The sidebar is used to have categories and the main section as a table (!) of items. When an item (=row) in this table is doubleClicked I want to navigate to a “detailsView” of that particular item. The first approach of the app used a List where dozens of examples are flying around how to use NavigationLink() and .navigationDestination with it, however I couldn’t find any working example for swiftUi Table().

The most logical approach seems to me to use “Table(of: …) {} rows: {ForEach}” appoach where in the ForEach loop the NavigationLinks could be added. But this didn’t work. Below an code example to ease the dicsussion:

struct TableView: View {
  @State private var items: [Item] = []

  @State private var sorting = [KeyPathComparator(item.model)]
  @State private var selection: item.ID?
  
  var body: some View {
    VStack{
      Table(of: Item.self, selection: $selection, sortOrder: $sorting) {
        TableColumn("Make",  value: .make)
        TableColumn("Model", value: .model)
        TableColumn("Date",  value: .date)
      } rows: {
        ForEach(items) { item in
          TableRow(item)
          NavigationLink(destination: DetailView(itemId: selection))
        }
      }
   }

However it seems this is not allowed, the compiler throws “No exact matches in reference to static method ‘buildExpression'” if I add the line of code with NavigationLink.

New contributor

SwiftyYns is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT