At WWDC21 in What's New in SwiftUI at 13:19, Apple introduced the Table container which allows the creation of multi-column tables on macOS using SwiftUI:
We can provide a binding to an array of sort descriptors to make the columns of a table sortable. Here I'm using my @FetchRequest's sortDescriptors for that:
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(sortDescriptors: [SortDescriptor(\.addDate, order: .reverse)], animation: .default)
private var videos: FetchedResults<Video>
@State
private var selection = Set<Video.ID>()
var body: some View {
NavigationView {
Table(videos, selection: $selection, sortOrder: $videos.sortDescriptors) { // <-- HERE
TableColumn("Title") {
Text($0.title)
}
TableColumn("Added") {
Text($0.addDate)
}.width(120)
TableColumn("Published") {
Text($0.publishedAt)
}.width(120)
TableColumn("Duration") {
Text($0.duration)
}.width(50)
}
}
}
}
While this compiles and runs without errors, the sorting does not work. I can click on any columns of the table, but nothing happens.
Am I missing something, or is this a known bug with Table on macOS?
