How to set TableColumn in Table using ForEach

         Table(tableData) {
          ForEach(tableColumns) { item in
            TableColumn(item.name) { data in
                  Text(data.value)
            }
          }
        }

Replies

What do you get ? What did you expect ?

The syntax includes a value:

struct Person: Identifiable {
  let givenName: String
  let familyName: String
  let id = UUID()
}

@State private var people = [
  Person(givenName: "Juan", familyName: "Chavez"),
  Person(givenName: "Mei", familyName: "Chen"),
  Person(givenName: "Tom", familyName: "Clark"),
  Person(givenName: "Gita", familyName: "Kumar"),
]

TableColumn("Given Name", value: \Person.givenName) { person in
    Text(person.givenName)
}

So you should likely have this:

            TableColumn(item.name, value : \.XXXXX.value) { data in
                  Text(data.value)
            }

where XXXX is the struct containing value.

  • What I mean is how to use ForEach to loop through variables to generate TableColumn based on data results when you're not sure what columns are available

Add a Comment

So you should have something like:

          ForEach(tableColumns) { item in
            TableColumn(item.name, value : \.XXXXX.value) { data in
                  Text(data.value)
            }
  • What is it ? Can we get a precise working example ?

Add a Comment

This is a very essential question since it would give us the possibilities to show/hide table columns dynamically which is not possible another way.

So I am asking the same. Can someone provide a complete working solution ?

Post not yet marked as solved Up vote reply of MTei Down vote reply of MTei

Having the same question, any ideas on this?

I have the same question.

Anybody find work solution?