SwiftUI Table Limit of Columns?

I am trying to build a Table using SwiftUI that has 14 columns. If I enter that many TableColumns under Table, I get the "The compiler is unable to type-check..." error. In fact, I get that error until I cut it back to 10 columns.

My Columns all look something like this, with different name and value paths, of course:

            TableColumn("Loc", value: \.location)

I have seen that the way around this error is to break up the expression into something simpler. But I can't figure out how to simplify the number of columns.

Any ideas?

Thanks,

Don

Post not yet marked as solved Up vote post of decarlile Down vote post of decarlile
1.3k views

Replies

SwiftUI's ViewBuilder is currently still limited to 10 views inside another view. If you want to put more views inside you have to group them using Group:

View1{
  Group{
   Subview1()
   ...
   SubView10()
  }
  Group{
    Subview11()
    ...
    Subview20()
  }
}
  • Thank you for that reply, and that answer. It doesn't appear, however, that Grouping TableColumns is useful. Putting the TableColumns into 2 Groups results in nothing being displayed. I guess I will need to revisit my UI and see if I can get by with 10 columns.

Add a Comment

I am attempting to overcome the 10 columns limitation of Table This is "Crashing" the compiler with this error:

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

Code:

import SwiftUI

//---------------------------------------------------------------------------------------------
struct TestStruct : Identifiable
{
    var     id          =   UUID ()
    var     name        :   String
    var     val         :   Int
}

//---------------------------------------------------------------------------------------------
struct ContentView: View
{
    @State  var     testData    :   [ TestStruct ] = [ TestStruct ( name: "Leopold", val: 1 ), TestStruct ( name: "Napoleon", val: 2 ) ]

    var body: some View
    {
        VStack
        {
            Table ( testData )
            {
                Group
                {
                    TableColumn ( "Name" ) { testStruct in Text ( testStruct.name ) }
                    TableColumn ( "Value" ) { testStruct in Text ( String ( testStruct.val ) ) }
                }
            }
        }
    }
}

//---------------------------------------------------------------------------------------------
struct ContentView_Previews: PreviewProvider
{
    static var previews: some View
    {
        ContentView ()
    }
}

Can Apple provide a working example with more than 10 columns ( e.g: using a ForEach ... ) , that would be perfect

Many thanks

I too am looking for > 10 columns (as well as the ability to edit cells). Any solutions would be appreciated.

One way to this is to use multiple TableColumnBuilder objects.

Using Group removes the ability to sort via the column headers, and fancy column customization was failing for me too. TableColumnBuilders also create more easily organized code.

https://developer.apple.com/documentation/swiftui/tablecolumnbuilder/

    @TableColumnBuilder<ObjectType, KeyPathComparator<ObjectType>>
    var tableColumns1: some TableColumnContent<ObjectType, KeyPathComparator<ObjectType>> {
        
        TableColumn(

...

        Table(of: ObjectType.self, selection: $selection, sortOrder: $sortOrder, columnCustomization: $columnCustomization) {
                
                tableColumns1
                
                tableColumns2
                
                tableColumns3


                
            } rows: {
                ForEach(filteredCompositions) { obj in
                    TableRow(obj)
                }
            }