因此,为了简单起见,我将首先描述代码的功能方式。我有一个列表,该列表是通过使用ForEach循环遍历AWS数据库而生成的。
List{
ForEach(self.data.database1){ row in
HStack {
Button("\((row.name)!)") {
self.selectedItem = row.id
self.selectedItemName = row.poi!
self.pressedItem = true
}
Spacer()
Button("Delete") {
self.selectedItem = row.id
self.showDeleteItemView = true
//this brings up a view that can confirm you want to delete
}.buttonStyle(BorderlessButtonStyle())
}
}
}
列表中的每一行都包含一个“删除”按钮。此删除按钮将打开以下视图:
if self.showDeleteEventView == true {
ConfirmActionView(showView: self.$showDeleteItemView, actionName: "delete this event", function: {
for item in self.data.database1{
if self.selectedItem == item.id{
DispatchQueue.main.async {
self.data.deleteItem(id: event.id)
self.reloadView.toggle()
}
}
}
}, buttonLabel: "Delete")
}
视图是:
struct ConfirmActionView: View {
@Binding var showView: Bool
var actionName: String
var function: () -> Void
var buttonLabel: String
var body: some View {
ZStack {
VStack {
HStack {
Spacer()
Button("X") {
self.showView = false
}
}
Text("Are you sure you want to \(self.actionName)?")
Spacer()
HStack {
Button("\(self.buttonLabel)") {
print("confirmed action")
self.function()
self.showView = false
}
Button("Cancel") {
self.showView = false
}
.padding(EdgeInsets(top: 6, leading: 6, bottom: 6, trailing: 6))
}
}.frame(width: 300, height: 150)
}
}
}
deleteItem()函数如下:
func deleteItem(id: Int) {
let baseUrl = URL(string: itemUrl)
let deletionUrl = baseUrl!.appendingPathComponent("\(id)")
print("Deletion URL with appended id: \(deletionUrl.absoluteString)")
var request = URLRequest(url: deletionUrl)
request.httpMethod = "DELETE"
print(token) // ensure this is correct
request.allHTTPHeaderFields = ["Authorization": "Token \(token)"]
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
print("Encountered network error: \(error)")
return
}
if let httpResponse = response as? HTTPURLResponse {
// this is basically also debugging code
print("Endpoint responded with status: \(httpResponse.statusCode)")
print(" with headers:\n\(httpResponse.allHeaderFields)")
}
// Debug output of the data:
if let data = data {
let payloadAsSimpleString = String(data: data, encoding: .utf8) ?? "(can't parse payload)"
print("Response contains payload\n\(payloadAsSimpleString)")
}
}
task.resume()
}
stateVariable1是用于保存行中项目名称的变量。 reloadView是一个@State布尔变量,因此我认为如果切换它,则应从数据库中删除该项目后刷新视图。我想像的代码功能一样,除了reloadView切换开关实际上并没有重新加载视图。
答案 0 :(得分:1)
如果您的数据可以更改,则需要动态变量ForEach
。
尝试更换:
List {
ForEach(self.data.database1) { row in
HStack {
...
具有:
List {
ForEach(self.data.database1, id: \.id) { row in
HStack {
...
编辑
在删除项目后,您的数据似乎也没有刷新-您从服务器中删除了它,但未从本地删除它。
您可以在删除项目后重新加载它:
func deleteItem(id: Int) {
...
let task = URLSession.shared.dataTask(with: request) { data, response, error in
...
if let data = data {
let payloadAsSimpleString = String(data: data, encoding: .utf8) ?? "(can't parse payload)"
print("Response contains payload\n\(payloadAsSimpleString)")
}
// here you can reload data from the server
// or pass function (which reloads data) as a parameter and call it here
}
task.resume()
}