本文介绍了带有 onDelete 和确认警报的 Swiftui WatchKit 列表无法正确更新 tableview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能指出我如何使用确认删除功能实现简单列表的正确方向,或者至少在这里展示最佳实践.

Can someone point me in the right direction on how to implement a simple list with a confirm remove-function or at least show best practice here.

如果警报部分被删除并且立即删除操作,下面的代码将起作用.不知何故,确认警报的呈现使得删除操作对错误的人员列表起作用.第一次删除也会给出控制台警告:

Below code will work if the alert-part is removed and delete action is immediate.Somehow, the presentation of the confirmation-alert makes the the deletion act on the wrong list of persons. First removal also gives a console warning:

[TableView] 仅警告一次:UITableView 被告知在不在视图层次结构中布局其可见单元格和其他内容(表视图或其父视图之一尚未添加到窗口中).这可能会通过强制 table view 中的视图在没有准确信息(例如 table view bounds、trait collection、layout margins、safe area insets 等)的情况下加载和执行布局而导致错误,并且还会由于额外的布局传递而导致不必要的性能开销.在 UITableViewAlertForLayoutOutsideViewHierarchy 处创建一个符号断点,以便在调试器中捕获此问题并查看导致此问题发生的原因,因此您可以尽可能避免此操作,或者将其推迟到将表视图添加到窗口中.

但是,我不知道如何在不删除警报的情况下解决此问题.顺便说一句,这个确切的代码在我相信我的 mac 更新 xcode 之前几周前就起作用了.

However, I have no idea on how to solve this without removing the alert. By the way, this exact code worked a couple of weeks ago before my mac updated xcode i believe.

import Foundation
import SwiftUI
import Combine

struct Person: Identifiable{
    var id: Int
    var name: String

    init(id: Int, name: String){
        self.id = id
        self.name = name
    }

}

class People: ObservableObject{
    @Published var people: [Person]


    init(){
        self.people = [
            Person(id: 1, name:"One"),
            Person(id: 2, name:"Two"),
            Person(id: 3, name:"Three"),
            Person(id: 4, name:"Four")]
    }

}

struct ContentView: View {
    @ObservedObject var mypeople: People = People()
    @State private var showConfirm = false
    @State private var idx = 0

    func setDeletIndex(at idxs:IndexSet) {
        self.showConfirm = true
        self.idx = idxs.first!
    }
    func delete() {
        self.mypeople.people.remove(at: idx)
    }
    var body: some View {
        VStack {
            List {

                Text("Currently \(mypeople.people.count) persons").font(.footnote)
                    .alert(isPresented: $showConfirm) {
                        Alert(title: Text("Delete"), message: Text("Sure?"),
                              primaryButton: .cancel(),
                              secondaryButton: .destructive(Text("Delete")) {

                                self.delete()
                            })
                }

                ForEach(mypeople.people){ person in
                    Text("\(person.name)")
                }.onDelete { self.setDeletIndex(at: $0) }
            }
        }
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

推荐答案

问题是由于更新alert closed &列表记录删除.解决方法是延迟删除,如下(使用 Xcode 11.4 测试)

The problem is due to conflict of updating alert closing & List record removing. The working workaround is to delay deleting, as below (tested with Xcode 11.4)

Text("Currently \(mypeople.people.count) persons").font(.footnote)
    .alert(isPresented: $showConfirm) {
        Alert(title: Text("Delete"), message: Text("Sure?"),
              primaryButton: .cancel(),
              secondaryButton: .destructive(Text("Delete")) {
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { // here !!
                    self.delete()
                }
            })
}

这篇关于带有 onDelete 和确认警报的 Swiftui WatchKit 列表无法正确更新 tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 05:43