TechnologiesSwiftUI

TableForEachContent struct

iOSmacOStvOSwatchOSvisionOS✓ renders

A type of table row content that creates table rows created by iterating

How it works

TableForEachContent is the table-row content that SwiftUI synthesizes when you drive a Table's rows with a ForEach. Rather than writing one TableRow per record by hand, you hand ForEach a collection and it produces a TableForEachContent value that conforms to TableRowContent, so the table can lay out, sort, and select across every element. Reach for it whenever a table's rows come from dynamic, data-driven content — a list of people, files, or any Identifiable model — instead of a fixed, statically declared set of rows.

  1. Open the rows builder with a Table

    A Table separates its columns from its rows: the trailing rows: closure is a @TableRowBuilder where row content lives. Here Table(of: Person.self) declares the element type up front, and the rows: builder is where a TableForEachContent value will be supplied.

  2. Produce the rows with ForEach over Identifiable data

    Placing ForEach(people) inside the rows: builder is what creates a TableForEachContent. Because Person is Identifiable (its id is a UUID), ForEach can give each row a stable identity, which the table uses to keep rows distinct across updates, selection, and sorting.

  3. Emit one TableRow per element

    The ForEach closure returns the per-element row, TableRow(person), binding that record to the row. TableForEachContent is the aggregate of all those TableRow values — it adapts the dynamic ForEach output into the static TableRowContent the Table expects, so each Person is matched against the declared columns.

  4. Resolve cells through the declared columns

    Each row produced by TableForEachContent is rendered against the table's TableColumn definitions. The TableColumn("Name") and TableColumn("Role") closures receive the bound person, pulling person.name and person.role into Text so the columns align with every row the ForEach emits.

Try it — Append another Person(name: "Dave", role: "Analyst") to the people array and the ForEach regenerates TableForEachContent with a fourth TableRow, so the table grows a row without touching the rows: builder.

Example & preview

Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.

TableForEachContent.swift
struct TableForEachContentDemo: View {
    struct Person: Identifiable {
        let id = UUID()
        let name: String
        let role: String
    }
    let people = [
        Person(name: "Alice", role: "Engineer"),
        Person(name: "Bob", role: "Designer"),
        Person(name: "Carol", role: "Manager")
    ]
    var body: some View {
        Table(of: Person.self) {
            TableColumn("Name") { person in
                Text(person.name)
            }
            TableColumn("Role") { person in
                Text(person.role)
            }
        } rows: {
            ForEach(people) { person in
                TableRow(person)
            }
        }
        .padding()
    }
}
Live preview
Name Role
Name Role
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →