Hi,i have been trying out SwiftUI Table and wanted to present a details view when click on Table Row occurs, but I couldn't figure out how to "deselect" row once its been selected, while it may not be what Table was intended for, but I still think this code should be valid. (iPadOS)
struct Person: Identifiable {
let givenName: String
let familyName: String
let emailAddress: String
let id = UUID()
}
private var people = [
Person(givenName: "Juan", familyName: "Chavez", emailAddress: "juanchavez@icloud.com"),
Person(givenName: "Mei", familyName: "Chen", emailAddress: "meichen@icloud.com"),
Person(givenName: "Tom", familyName: "Clark", emailAddress: "tomclark@icloud.com"),
Person(givenName: "Gita", familyName: "Kumar", emailAddress: "gitakumar@icloud.com")
]
@State private var selectedPeople: Person.ID?
@State private var detailsViewPresented: Bool = false
var body: some View {
Table(people, selection: $selectedPeople) {
TableColumn("Given Name", value: \.givenName)
TableColumn("Family Name", value: \.familyName)
TableColumn("E-Mail Address", value: \.emailAddress)
}
.onChange(of: selectedPeople) { selection in
guard selection != nil else {
return
}
detailsViewPresented = true
}
.sheet(isPresented: $detailsViewPresented, onDismiss: {
// Trying to reset the selection
self.selectedPeople = nil
}) {
Text("Person's details")
}
}
Here when I press row, it gets selected and Text is presented, but row still remains selected, and yes, I could just use onTapGesture within row content if I declared TableColumn with explicit content, but it would just be added to that column and would not provide build in selection style.
https://developer.apple.com/documentation/swiftui/table