swiftmacosswiftui

macOS SwiftUI Table with more than 10 columns?


I've tried to add the Group {} trick to get more than 10 elements in a Table view, but the fails to compile just like when there is more than 10 elements without the group.

    var body: some View {
      Table(viewModel.tableArrayOfStructs, sortOrder: $sortOrder) {
        Group {
          TableColumn("One", value: \.One).width(min: 35, ideal: 35, max:   60)
          TableColumn("Two", value: \.Two).width(30)
          TableColumn("Three", value: \.Three).width(50)
          TableColumn("Four", value: \.Four).width(min: 150, ideal: 200, max: nil)
          TableColumn("Five", value: \.Five).width(50)
          TableColumn("Six", value: \.Six).width(min: 50, ideal: 55, max: nil)
          TableColumn("Seven", value: \.Seven).width(88)
          TableColumn("Eight", value: \.Eight).width(88)
          TableColumn("Nine", value: \.Nine).width(20)
          TableColumn("Ten", value: \.Ten).width(50)
       }
       TableColumn("Eleven", value: \.Eleven).width(50)

    }

If I add the eleventh+ column also into a new group I get the same issue. The compiler reports:

The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

Is there any means to have a table with more than 10 columns short of dropping down to NSViewRepresentable?


Solution

  • Apple Developer Technical Support was able to help. They recommended being explicit with the value: type information in the TableColumn to give the compiler some help. Without explicit type info with or without the group the compiler simply gives up. I also found that once you add one group, you need to add all TableColumns to be within groups (which have 10 or less TableColums per group)

    var body: some View {
      Table(viewModel.tableArrayOfStructs, sortOrder: $sortOrder) {
        Group {
          TableColumn("One", value: \ViewModel.TableArrayOfStructs.One).width(min: 35, ideal: 35, max:   60)
          TableColumn("Two", value: \ViewModel.TableArrayOfStructs.Two).width(30)
          TableColumn("Three", value: \ViewModel.TableArrayOfStructs.Three).width(50)
          TableColumn("Four", value: \ViewModel.TableArrayOfStructs.Four).width(min: 150, ideal: 200, max: nil)
          TableColumn("Five", value: \ViewModel.TableArrayOfStructs.Five).width(50)
          TableColumn("Six", value: \ViewModel.TableArrayOfStructs.Six).width(min: 50, ideal: 55, max: nil)
          TableColumn("Seven", value: \ViewModel.TableArrayOfStructs.Seven).width(88)
          TableColumn("Eight", value: \ViewModel.TableArrayOfStructs.Eight).width(88)
          TableColumn("Nine", value: \ViewModel.TableArrayOfStructs.Nine).width(20)
          TableColumn("Ten", value: \ViewModel.TableArrayOfStructs.Ten).width(50)
       }
       Group {
         TableColumn("Eleven", value: \ViewModel.TableArrayOfStructs.Eleven).width(50)
       }
    
    }