如何在SwiftUI中从列表中获取已删除行的索引?

km0tfn4u  于 2023-05-05  发布在  Swift
关注(0)|答案(1)|浏览(145)

我想使用ForEach从显示为列表的数组中删除一个元素,但我还需要向REST API发送HTTP请求,并且需要将元素的索引放在请求的主体中。下面是我的代码:

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

下面是delete函数:

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

gab6jxml1#

如果你删除by-one,那么下面给予你删除行的索引

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
}

相关问题