how to find selected row array index in Table

I have Array of 4 items, and I show 2 of them in table.

And selecting the item in table, I want to print out selected row all items from array.

It works only if I have not sorted the table first.

As soon I sort the table I can't find reference to the array index.

Is there any way to find array index?

sample:


struct testtable: View {

struct Person: Identifiable {

    let givenName: String

    let familyName: String

    let index: String

    let id: Int

}

@State private var people = [

    Person(givenName: "Juan", familyName: "Chavez", index: "0", id: 0),

    Person(givenName: "Mei", familyName: "Chen", index: "1", id: 1),

    Person(givenName: "Tom", familyName: "Clark", index: "2", id: 2),

    Person(givenName: "Gita", familyName: "Kumar", index: "3", id: 3),

]

@State private var sortOrder = [KeyPathComparator(\Person.givenName)]

@State private var selection : Person.ID?

    var body: some View {

    Button(action: {

        print("array[\(self.selection)]")

        let arrayindex: Int = self.selection!

        print(people[arrayindex])

    }){

        Label("Print Array Items", systemImage: "doc.text.magnifyingglass")}

    

    

    Table(people, selection: $selection, sortOrder: $sortOrder) {

        TableColumn("Given Name", value: \.givenName)

        TableColumn("Family Name", value: \.familyName)

    }

    .onChange(of: sortOrder) {

        people.sort(using: $0)

    }

}

}
Answered by Claude31 in 709788022

Problem is that index is wrong.

struct TestTable: View {
    
    struct Person: Identifiable {
        let givenName: String
        let familyName: String
        // let index: String
        let id = UUID() //: Int
        
    }
    
    @State private var people = [
        Person(givenName: "Juan", familyName: "Chavez"), // , index: "0"), //, id: 0),
        Person(givenName: "Mei", familyName: "Chen"), // , index: "1"), //, id: 1),
        Person(givenName: "Tom", familyName: "Clark"), // , index: "2"), //, id: 2),
        Person(givenName: "Gita", familyName: "Kumar") // , index: "3") //, id: 3),
    ]
    
    @State private var sortOrder = [KeyPathComparator(\Person.givenName)]
    
    @State private var selection = Set<Person.ID>()
    
    var body: some View {
        
        Button(action: {
            
            let arrayindex = self.selection
            let item = people.filter() { arrayindex.contains($0.id) }
            print(item.first?.givenName ?? "")

            // If you need the row:
            var newIndex : Int?
            for (index, elem) in people.enumerated() {
                if arrayindex.contains(elem.id) { newIndex = index; break }
            }
            print("index in table is", newIndex == nil ? "unknown" : String(newIndex!))

        }){
            Label("Print Array Items", systemImage: "doc.text.magnifyingglass")}
        
        
        Table(people, selection: $selection, sortOrder: $sortOrder) {
            TableColumn("Given Name", value: \.givenName)
            TableColumn("Family Name", value: \.familyName)
            
        }
        
        .onChange(of: sortOrder) {
            people.sort(using: $0)
            
        }
        
    }
    
}
Accepted Answer

Problem is that index is wrong.

struct TestTable: View {
    
    struct Person: Identifiable {
        let givenName: String
        let familyName: String
        // let index: String
        let id = UUID() //: Int
        
    }
    
    @State private var people = [
        Person(givenName: "Juan", familyName: "Chavez"), // , index: "0"), //, id: 0),
        Person(givenName: "Mei", familyName: "Chen"), // , index: "1"), //, id: 1),
        Person(givenName: "Tom", familyName: "Clark"), // , index: "2"), //, id: 2),
        Person(givenName: "Gita", familyName: "Kumar") // , index: "3") //, id: 3),
    ]
    
    @State private var sortOrder = [KeyPathComparator(\Person.givenName)]
    
    @State private var selection = Set<Person.ID>()
    
    var body: some View {
        
        Button(action: {
            
            let arrayindex = self.selection
            let item = people.filter() { arrayindex.contains($0.id) }
            print(item.first?.givenName ?? "")

            // If you need the row:
            var newIndex : Int?
            for (index, elem) in people.enumerated() {
                if arrayindex.contains(elem.id) { newIndex = index; break }
            }
            print("index in table is", newIndex == nil ? "unknown" : String(newIndex!))

        }){
            Label("Print Array Items", systemImage: "doc.text.magnifyingglass")}
        
        
        Table(people, selection: $selection, sortOrder: $sortOrder) {
            TableColumn("Given Name", value: \.givenName)
            TableColumn("Family Name", value: \.familyName)
            
        }
        
        .onChange(of: sortOrder) {
            people.sort(using: $0)
            
        }
        
    }
    
}

Thank you :) I also reached to the same conclusion. However I solved it so:

struct testtable: View {





struct Person: Identifiable {

    let givenName: String

    let familyName: String

    let index: String

    let id = UUID()

}

@State private var people = [

    Person(givenName: "Juan", familyName: "Chavez", index: "0"),

    Person(givenName: "Mei", familyName: "Chen", index: "1"),

    Person(givenName: "Tom", familyName: "Clark", index: "2, id: 2"),

    Person(givenName: "Gita", familyName: "Kumar", index: "3, id: 3"),

]

@State private var sortOrder = [KeyPathComparator(\Person.givenName)]

    @State private var selection : Person.ID?

    var body: some View {

    Button(action: {

        if self.selection != nil {

        guard let select = people.first(where: { $0.id == self.selection

            }) else {

                fatalError()

            }

        print("array[\(self.selection)]")

        

        print(select)

        }

    }){

        Label("Print Array Items", systemImage: "doc.text.magnifyingglass")}

    

    

    Table(people, selection: $selection, sortOrder: $sortOrder) {

        TableColumn("Given Name", value: \.givenName)

        TableColumn("Family Name", value: \.familyName)

    }

    .onChange(of: sortOrder) {

        people.sort(using: $0)

    }

}

}

how to find selected row array index in Table
 
 
Q