How to get the index of the element in the List in SwiftUI when the List is populated with the array?

后端 未结 1 954
暗喜
暗喜 2020-12-11 21:35

In my SwiftUI app, I have a list of items.

I\'m using the array of MenuItems to fill in the list

struct MenuItem: Identifiable, Equatable {
                 


        
相关标签:
1条回答
  • 2020-12-11 22:15

    This can be done using using .enumerated. For your MenuItem values it will be as follows

    List {
        ForEach(Array(menuItems.enumerated()), id: \.1.id) { (index, textItem) in
            // do with `index` anything needed here
            Text(textItem.text)
        }
    }
    
    0 讨论(0)
提交回复
热议问题