Using a TableColumn requires a Table {// content } rows: { // data source } pattern similar to below. Where in the example rowItems is a collection of Items for the data source.
Items
struct Item {
let from: Date
let to: Date
let hours: Double
}
// The table view
Table(selection: $selection, sortOrder: $sortOrder) {
// Sort column on \.from key value path of item
TableColumn("From", value:\.from) { item in
Text(item.from(date: .numeric, time: .omitted))
}
// Sort column on \.to key value path of item
TableColumn("To", value:\.to) { item in
Text(item.to.formatted(date: .numeric, time: .omitted))
}
// Sort column on \.hours key value path of item
TableColumn("Total", value:\.hours) { item in
Text(item.hours)
}
} rows: {
// Populate the rows data source with TableRow types containing an item type from the rowItems collection which is passed on each TableColumm.
ForEach(rowItems) { item in
TableRow(item)
}
}
See Apple example code here: https://developer.apple.com/documentation/swiftui/building_a_great_mac_app_with_swiftui