Get index in ForEach in SwiftUI

前端 未结 6 1893
执笔经年
执笔经年 2020-12-24 05:01

I have an array and I want to iterate through it initialize views based on array value, and want to perform action based on array item index

When I iterate through o

6条回答
  •  轮回少年
    2020-12-24 05:53

    Here is a simple solution though quite inefficient to the ones above..

    In your Tap Action, pass through your item

    .tapAction {
    
       var index = self.getPosition(item)
    
    }
    

    Then create a function the finds the index of that item by comparing the id

    func getPosition(item: Item) -> Int {
    
      for i in 0..

提交回复
热议问题