TableColumnContent

A type used to represent columns within a table.

ProtocoliOS 16.0+macOS 12.0+TableColumnContent in SwiftUI

Declaration

React
type TableColumnContent = SwiftOpaque<'TableColumnContent'>;
Swift
@MainActor @preconcurrency protocol TableColumnContent<TableRowValue, TableColumnSortComparator>

Overview

This type provides the body content of the column, as well as the types of the column's row values and the comparator used to sort rows.

You can factor column content out into separate types or properties, or by creating a custom type conforming to TableColumnContent.

var body: some View {
    Table(people, selection: $selectedPeople, sortOrder: $sortOrder) {
        nameColumns

        TableColumn("Location", value: \.location) {
            LocationView($0.location)
        }
    }
}

@TableColumnBuilder<Person, KeyPathComparator<Person>>
private var nameColumns: some TableColumnContent<
    Person, KeyPathComparator<Person>
> {
    TableColumn("First Name", value: \.firstName) {
        PrimaryColumnView(person: $0)
    }
    TableColumn("Last Name", value: \.lastName)
    TableColumn("Nickname", value: \.nickname)
}

The above example factors three table columns into a separate computed property that has an opaque type. The property's primary associated type TableRowValue is a Person and its associated type TableColumnSortComparator is a key comparator for the Person type.

A type conforming to this protocol inherits @preconcurrency @MainActor isolation from the protocol if the conformance is included in the type's base declaration:

struct MyCustomType: Transition {
    // `@preconcurrency @MainActor` isolation by default
}

Isolation to the main actor is the default, but it's not required. Declare the conformance in an extension to opt out of main actor isolation:

extension MyCustomType: Transition {
    // `nonisolated` by default
}

From Apple’s SwiftUI documentation. Code samples there are Swift.

See also

More in Lists and tables.