Structure

Table

A container that presents rows of data arranged in one or more columns, optionally providing the ability to select one or more members.

Declaration 宣言

struct Table<Value, Rows, Columns> where Value == Rows.TableRowValue, Rows : TableRowContent, Columns : TableColumnContent, Rows.TableRowValue == Columns.TableRowValue

Overview 概要

You commonly create tables from collections of data. The following example shows how to create a simple, two-column table from an array of a Person type that conforms to Identifiable:


struct Person: Identifiable {
    let givenName: String
    let familyName: String
    let id = UUID()
}
private var people = [
    Person(givenName: "Juan", familyName: "Chavez"),
    Person(givenName: "Mei", familyName: "Chen"),
    Person(givenName: "Tom", familyName: "Clark"),
    Person(givenName: "Gita", familyName: "Kumar"),
]


var body: some View {
    Table(people) {
        TableColumn("Given Name", value: \.givenName)
        TableColumn("Family Name", value: \.familyName)
    }
}

Supporting Selection in Tables

To make rows of a table selectable, provide a binding to a selection variable. Binding to a single instance of the table data’s Identifiable.ID type creates a single-selection table. Binding to a Set creates a table that supports multiple selections. The following example shows how to add multi-select to the previous example. A Text view below the table shows the number of items currently selected.


struct Person: Identifiable {
    let givenName: String
    let familyName: String
    let id = UUID()
}
private var people = [
    Person(givenName: "Juan", familyName: "Chavez"),
    Person(givenName: "Mei", familyName: "Chen"),
    Person(givenName: "Tom", familyName: "Clark"),
    Person(givenName: "Gita", familyName: "Kumar"),
]
@State private var selectedPeople = Set<Person.ID>()


var body: some View {
    Table(people, selection: $selectedPeople) {
        TableColumn("Given Name", value: \.givenName)
        TableColumn("Family Name", value: \.familyName)
    }
    Text("\(selectedPeople.count) people selected")
}

Supporting Sorting in Tables

To make the columns of a table sortable, provide a binding to an array of sort descriptors. The table will reflect the sorted state through its column headers, allowing sorting for any columns with key paths. Updates to the sort descriptors should be reflected in the collection of data provided to the table, as the table does not itself perform a sort operation. The following example shows how to add sorting capability to the previous example:


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"),
]
@State private var sortOrder = [KeyPathComparator(\Person.givenName)]


var body: some View {
    Table(people, sortOrder: $sortOrder) {
        TableColumn("Given Name", value: \.givenName)
        TableColumn("Family Name", value: \.familyName)
    }
    .onChange(of: sortOrder) {
        people.sort(using: $0)
    }
}

Topics 話題

Creating a Table from Columns

Creating a Sortable Table from Columns

Creating a Table from Columns and Rows

Creating a Sortable Table from Columns and Rows

Styling a Table

Supporting Types 支援を行う型

Default Implementations 省略時実装

Relationships 関係

Conforms To 次に準拠