SwiftUI Table scrollTo
11:40 07 Aug 2022

Does anyone know if there's a system to scroll to a row in a SwiftUI Table? I know I can use a List/ScrollView/ScrollViewReader combination, but I'm ideally looking for something that works with Table. I did try using a ScrollViewReader around a Table, but that did nothing

https://developer.apple.com/documentation/SwiftUI/Table

I'm ideally looking for a method on the Table, or something like a TableReader

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")
.... 100 more....
]

var body: some View {
  ScrollViewReader { proxy
    Table(people) {
        TableColumn("Given Name", value: \.givenName)
        TableColumn("Family Name", value: \.familyName)
        TableColumn("E-Mail Address", value: \.emailAddress)
    }
    .onChange(of: selection) { newValue in
        proxy.scrollTo(newValue, anchor: .center)
      }
  }
}
swiftui