本文介绍了如何从 SwiftUI 的列表中获取已删除行的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从使用 ForEach
显示为列表的数组中删除一个元素,但我还需要向 发送一个
并且我需要将元素的索引放在请求正文中.这是我的代码:HTTP
请求>REST API
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)
这里是删除函数:
func delete(at offsets: IndexSet) {
self.symptoms.remove(atOffsets: offsets)
// here I want to make the HTTP request
}
推荐答案
如果你逐个删除,那么下面给你删除行的索引
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
}
这篇关于如何从 SwiftUI 的列表中获取已删除行的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!