GlassTable
System Table where available and a List elsewhere, with typed sorting and selection.
- System control
- iOS 26 · iPadOS 26 · macOS 26
- Version 1.1.0
glass-table
Drawn by the system. GlassKit configures it and leaves the glass to the system.
CaptureInstallation
spglass add glass-tableCopies the sources and their dependencies into your project. Set up the CLI.
.package(url: "https://git.spboucher.ai/sp-glasskit.git", from: "1.1.0")
import SPGlassKit| 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
ContentUnavailableViewwithemptyTitle. - 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
.isSelectedtrait 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
.isSelectedtrait 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
Tabledirectly. - 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
bodyon every update (O(n log n)); for very large collections sort in the model and passsort: .constant(nil). - The system
Tableshows only its first column in compact width, which is why GlassTable switches to a list there. The switch followshorizontalSizeClassand was compiled for iOS but not exercised on a device in this phase. - Column widths are left to the system.
Example
import SwiftUI
import SPGlassKit
public struct GlassTableExample: GlassExample {
public static let componentID = "glass-table"
struct Station: Identifiable {
let id: String
let name: String
let line: String
let dailyRiders: Int
let opened: Int
}
// Fictitious data.
private let stations = [
Station(id: "s1", name: "Harbour Square", line: "Blue", dailyRiders: 48_200, opened: 1987),
Station(id: "s2", name: "Élan–Université", line: "Green", dailyRiders: 31_750, opened: 2004),
Station(id: "s3", name: "North Mills", line: "Blue", dailyRiders: 9_640, opened: 1991),
Station(id: "s4", name: "Parc des Érables", line: "Orange", dailyRiders: 22_310, opened: 2016),
Station(id: "s5", name: "Old Foundry", line: "Green", dailyRiders: 9_640, opened: 1979),
]
private let columns: [GlassTableColumn<Station>] = [
GlassTableColumn("Station", id: "name") { $0.name },
GlassTableColumn("Line", id: "line") { $0.line }
.badged { _ in .neutral },
GlassTableColumn("Daily riders", id: "riders", sortingBy: { $0.dailyRiders }) { $0.dailyRiders.formatted(.number) }
.aligned(.trailing),
GlassTableColumn("Opened", id: "opened", sortingBy: { $0.opened }) { $0.opened.formatted(.number.grouping(.never)) }
.aligned(.trailing),
]
@State private var sort: GlassTableSort? = GlassTableSort(columnID: "riders", order: .reverse)
@State private var selection: Set<String> = ["s2"]
@State private var showsEmpty = false
public init() {}
public var body: some View {
VStack(alignment: .leading, spacing: 12) {
Toggle("Show the empty state", isOn: $showsEmpty)
Text("\(selection.count) selected")
.font(.callout)
.foregroundStyle(.secondary)
GlassTable(showsEmpty ? [] : stations, columns: columns, sort: $sort, selection: .multiple($selection), emptyTitle: "No station")
.frame(minHeight: 320)
}
.padding()
}
}
#Preview("GlassTable") { GlassTableExample() }
Registry entry
| Identifier | glass-table |
|---|---|
| Version | 1.1.0 |
| Depends on | glass-badge, glass-components-support, glass-foundation |
| Frameworks | SwiftUI |
| Public symbols | GlassTable, GlassTableColumnAlignment, GlassTableColumn, GlassTableSort, GlassTableSelection, GlassTableLogic |
| Licence | MIT |
| Manifest | manifest.json |
Files
Data/Data/GlassTable.swift18984 bytes · sha256 d093e02df8d3…Data/Data/GlassTableModel.swift9709 bytes · sha256 aaf55b78adb1…