How to get the index of a deleted row from a list in SwiftUI?

萝らか妹 提交于 2021-02-20 18:47:14

问题


I want to delete an element from an array that I am displaying as a list using a ForEach, but I also need to send a HTTP request to a REST API and I need to put the index of the element in the body of the request. Here is my code:

ForEach(self.symptoms, id: \.self) { symptom in
           VStack(alignment: .leading) {
                    Text(symptom)
            }
}.onDelete(perform: delete)

Here is the delete function:

func delete(at offsets: IndexSet) {     
      self.symptoms.remove(atOffsets: offsets)
      // here I want to make the HTTP request
}

回答1:


If you remove by-one, then the following give you index of deleted row

func delete(at offsets: IndexSet) {     
      self.symptoms.remove(atOffsets: offsets)

      // here I want to make the HTTP request
      let index = offsets[offsets.startIndex]

      // ... use index in HTTP request
}


来源:https://stackoverflow.com/questions/61562137/how-to-get-the-index-of-a-deleted-row-from-a-list-in-swiftui

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!