# GlassTable
|
|
| Identifier |
glass-table |
| Module |
SPGlassData |
| Kind |
native-wrapper — the system Table on macOS and regular-width iPad, a system List in compact width |
| Platforms |
iOS 26, iPadOS 26, macOS 26 |
| Example |
Examples/Gallery/Data/GlassTableExample.swift |
# Minimal usage
GlassTable(cities, columns: [
GlassTableColumn("Name", id: "name") { $0.name },
GlassTableColumn("Population", id: "population", sortingBy: { $0.population }) {
$0.population.formatted(.number)
},
])
Rows are any Identifiable values. The table never modifies the host's collection: it sorts a copy for display.
# Variants
| Variant |
Use |
| Native table |
macOS, and iPad or iPhone in regular width. Sortable headers, system selection. |
| Compact list |
iPhone and narrow iPad windows. The first column is the row's main text, the other columns are labelled values, and a "Sort by" menu in the section header replaces the headers. |
selection: .none |
Rows are not selectable (default). |
selection: .single($id) |
At most one row. In the list form, tapping the selected row clears it. |
selection: .multiple($ids) |
Any number of rows. |
Columns are values:
| Initialiser |
Sorting |
GlassTableColumn(title, id:, text:) |
By the displayed text, with localizedStandardCompare (case-insensitive, numbers by value). |
GlassTableColumn(title, id:, sortingBy:, text:) |
By a typed Comparable value (number, date), while displaying formatted text. |
GlassTableColumn(title, id:, text:, comparator:) |
By a custom comparison returning ComparisonResult. |
# Parameters
| Parameter |
Type |
Default |
Meaning |
rows |
Sequence of Identifiable |
— |
The rows, in the host's order. |
columns |
[GlassTableColumn<Row>] |
— |
Columns, leading to trailing. |
sort |
Binding<GlassTableSort?> |
.constant(nil) |
Sorted column and direction; nil keeps the host's order. |
selection |
GlassTableSelection<Row.ID> |
.none |
Selection mode and binding. |
emptyTitle |
LocalizedStringKey? |
nil |
Text of the empty state; defaults to the localised "No data". |
GlassTableColumn.title is a LocalizedStringResource, looked up in the host application's strings.
# Column options and the compact table
| Modifier |
Meaning |
column.aligned(.trailing) |
Trailing alignment and tabular digits, for numbers. |
column.badged { row in tone } |
Draws the cell's text as a soft GlassBadge of the returned tone; nil keeps plain text. |
In compact width, a table of up to four columns is drawn as a dense table on an opaque surface: uppercase sortable headers with a direction chevron, hairlines, the first column emphasised, and a selected row marked by an accent edge, a tint and a checkmark. A short table takes its own height; a long one scrolls and stays lazy. With more than four columns, or at accessibility text sizes, each row becomes a stack of labelled values in a List, as before. GlassTableLogic.usesDenseCompactForm(columnCount:isAccessibilitySize:) and columnWeights(count:) are pure and public.
# States
- Empty: a
ContentUnavailableView with emptyTitle.
- Sorted: the header shows the system sort indicator; in the list form the menu shows the sorted column with a chevron and exposes "Sorted ascending / descending" as its accessibility value.
- Selected: system highlight in the table; a checkmark and the
.isSelected trait in the list.
- Pressed, focused and disabled (
.disabled(_:)) are drawn by the system.
# Advanced example
struct Invoice: Identifiable { let id: UUID; let client: String; let due: Date; let amount: Decimal }
@State private var sort: GlassTableSort? = GlassTableSort(columnID: "due")
@State private var selected: Invoice.ID?
GlassTable(invoices, columns: [
GlassTableColumn("Client", id: "client") { $0.client },
GlassTableColumn("Due", id: "due", sortingBy: { $0.due }) { $0.due.formatted(date: .abbreviated, time: .omitted) },
GlassTableColumn("Amount", id: "amount", sortingBy: { $0.amount }) { $0.amount.formatted(.currency(code: "CAD")) },
], sort: $sort, selection: .single($selected), emptyTitle: "No invoice")
// The same pure function is available to sort data elsewhere (export, tests):
let ordered = GlassTableLogic.sorted(invoices, by: sort, columns: columns)
# Accessibility
- The native table keeps the system's table semantics, keyboard navigation and VoiceOver column reading.
- In the list form each row is one element combining the main text and the labelled values; selection is exposed by the
.isSelected trait and a checkmark, not by colour.
- The sort direction is given by a symbol and by text ("Sorted ascending").
- Text follows Dynamic Type; values are formatted by the host with
FormatStyle.
# Limits
- Cells are text. A table with custom cell views should use the system
Table directly.
- One sort key at a time: the system table's secondary sort keys are not exposed. The sort is stable, so sorting successively by two columns gives a two-level order.
- Rows are sorted in
body on every update (O(n log n)); for very large collections sort in the model and pass sort: .constant(nil).
- The system
Table shows only its first column in compact width, which is why GlassTable switches to a list there. The switch follows horizontalSizeClass and was compiled for iOS but not exercised on a device in this phase.
- Column widths are left to the system.