TechnologiesSwiftUI

TableColumn struct

iOSmacOStvOSwatchOSvisionOS✓ renders

A column that displays a view for each row in a table.

How it works

TableColumn describes a single column of a Table, pairing a header with a rule for producing each row's cell from the table's underlying data. You declare one TableColumn per field you want to display, and the table draws the rows by applying each column to every element in its collection. Reach for it whenever you need to present a collection of structured records as a sortable, multi-column grid rather than a flat list.

  1. Declare columns inside a Table's content builder

    A Table takes its columns from a closure built with the @TableColumnBuilder result builder, so you list each TableColumn in sequence as the table's content. Here Table(people) opens that builder and the two TableColumn entries become the grid's columns, in order, left to right.

  2. Give each column a header with the title initializer

    The first argument to TableColumn is the column's header label, supplied as a string. The two columns are titled "Name" and "Role", and those strings render as the header row above their respective cells.

  3. Bind a column to a property with the value key path

    The value: parameter takes a KeyPath from a row element to a Comparable property; the table reads that property for every row to fill the column's cells and to know how to sort by it. TableColumn("Name", value: \.name) projects each Person's name, and \.role projects its role — no per-cell view code required because both properties are strings.

  4. Let the row type drive the column's data

    Each TableColumn resolves its key path against the element type of the table's collection, so the row type must be Identifiable for the table to track rows. The Person struct conforms to Identifiable via its id, and \.name / \.role are key paths into that same Person, which is why the columns line up with the people array.

Try it — Add a third line TableColumn("ID", value: \.id.uuidString) after the "Role" column to see a new column appear and the row data flow through it.

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.

TableColumn.swift
struct TableColumnDemo: View {
    struct Person: Identifiable {
        let id = UUID()
        let name: String
        let role: String
    }

    let people = [
        Person(name: "Ada Lovelace", role: "Engineer"),
        Person(name: "Alan Turing", role: "Scientist"),
        Person(name: "Grace Hopper", role: "Admiral")
    ]

    var body: some View {
        Table(people) {
            TableColumn("Name", value: \.name)
            TableColumn("Role", value: \.role)
        }
        .padding()
    }
}
Live preview
Name Role Ada Lovelace Engineer Alan Turing Scientist Grace Hopper Admiral
Name Role Ada Lovelace Engineer Alan Turing Scientist Grace Hopper Admiral
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →