0

I can't make ScrollViewReader to scroll on my List(). I have read many discussions such this or this

I use a model with Identifiable protocol and id as Int:

struct Country: Identifiable, Codable, Hashable {

        // database fields
        var id: Int64?
        var name: String
}

My view is currently like that:

            ScrollViewReader { proxy in
                    VStack {
                            Button(action: {
                                    proxy.scrollTo(38) // just a test
                            })
                            { Text("Jump to") }
                                 
                            List() {
                                    ForEach(countries) { country in

                                            Button (action: {
                                                    wine.countryId = country.id
                                                    // pop
                                                    self.mode.wrappedValue.dismiss()
                                            }) {
                                                    // cell content
                                                    HStack {
                                                            Image(country.flag).resizable().frame(width: 24.0, height: 24.0)
                                                            Text(country.name)
                                                    }
                                            }
                                            //.id(country.id)
                                       }
                                }
                        
                        }
                }
    }

I tried using an .id() to identify each row/button without success too. I don't know if I need id() as ForEach() use the identifiable protocol of the country to identify each item.

One important thing: countries are NOT displayed in the logical order (1,2,3...) of their var id. But even with the id(country.id) modifier, it never scroll to the right row/button of the list.

Most of examples we can find on the web use a simple index iteration for rows, and not a real model with struct().

alex.bour
  • 2,842
  • 9
  • 40
  • 66

1 Answers1

3

Types of matched identifiers must be the same, so as your id is Optional then in scrollTo it should be optional as well, like

Button(action: {
   proxy.scrollTo(Optional(Int64(38))) // << here !!
})

Tested with your replicated snapshot on Xcode 13 / iOS 15

Asperi
  • 228,894
  • 20
  • 464
  • 690
  • Thanks Asperi, It's not working here with same config Xcode 13 / iOS 15. Nothing appends when I tap the button. – alex.bour Oct 19 '21 at 09:22
  • 2
    I don't understand what do you mean by "nothing appends" - the demo is for your "Jump to" button, other your snapshot does not have anything related to *append*. So I can assume that it is about some different issue. ... Probably in your real code type casting from `Int` to `Optional(Int64)` is also needed. Again, main rule is **types must be the same** – Asperi Oct 19 '21 at 09:51