SwiftUI 列表未更新

ele*_*tai 5 macos swiftui

当我运行下面的代码并单击某个帐户时,它会显示与该帐户关联的所有日记行。如果我单击添加按钮,它会使用新的日志行更新日志行列表。但只有一次。当我再次单击它时,它会增加余额,但不会更新行列表。如果您单击另一个帐户,然后单击返回,您将看到完整列表。但它只自动更新列表一次?

我尝试account.lines在列表中替换为self.shared.journalLines.filter({$0.accountID == account.id})但我得到了相同的行为。

这是一个错误还是我错过了什么?如果这是一个错误,解决方法是什么?

import SwiftUI

struct ContentView: View {

    @EnvironmentObject var shared:SharedObject

    var body: some View {
        NavigationView {
            VStack {
                Button(action: {self.shared.test()}) {Text("Add")}
                List(self.shared.accounts) { account in
                    NavigationLink(destination:
                        List(self.shared.journalLines.filter({$0.accountID == account.id})) { line in
                            HStack {
                                Text(line.payee)
                                Spacer()
                                Text("$ \(line.amount, specifier: "%.2f")")
                            }
                        }.frame(maxWidth: .infinity, maxHeight: .infinity)
                    ) {
                        HStack {
                            Text(account.name)
                            Spacer()
                            Text("$ \(account.balance, specifier: "%.2f")")
                        }
                    }
                }
                .frame(width: 200)
            }
        }.frame(maxWidth: .infinity, maxHeight: .infinity)
    }

}

struct Account:Identifiable {
    @ObservedObject var shared:SharedObject
    var id:Int
    var name:String
    var lines:[JournalLine] {
        return self.shared.journalLines.filter({$0.accountID == self.id})
    }
    var balance:Double {
        return self.lines.map({$0.amount}).reduce(0){$0+$1}
    }
}
struct JournalLine:Identifiable {
    @ObservedObject var shared:SharedObject
    let id:UUID = UUID()
    var payee:String
    var accountID:Int
    var amount:Double
}

class SharedObject:ObservableObject {
    @Published var accounts:[Account] = []
    @Published var journalLines:[JournalLine] = []

    init() {

        self.accounts.append(Account(shared: self,id: 1, name: "Account#1"))
        self.accounts.append(Account(shared: self,id: 2, name: "Account#2"))
        self.accounts.append(Account(shared: self,id: 3, name: "Account#3"))

        self.journalLines.append(JournalLine(shared: self, payee: "Payee1", accountID: 1, amount: 100))
        self.journalLines.append(JournalLine(shared: self, payee: "Payee2", accountID: 1, amount: 200))
        self.journalLines.append(JournalLine(shared: self, payee: "Payee3", accountID: 2, amount: 300))
        self.journalLines.append(JournalLine(shared: self, payee: "Payee4", accountID: 2, amount: 400))
        self.journalLines.append(JournalLine(shared: self, payee: "Payee5", accountID: 3, amount: 500))
        self.journalLines.append(JournalLine(shared: self, payee: "Payee6", accountID: 3, amount: 600))

    }

    func test() {
        self.journalLines.append(JournalLine(shared: self, payee: "Payee10", accountID: 1, amount: 100))
        self.journalLines.append(JournalLine(shared: self, payee: "Payee10", accountID: 2, amount: 100))
        self.journalLines.append(JournalLine(shared: self, payee: "Payee10", accountID: 3, amount: 100))
    }

}
Run Code Online (Sandbox Code Playgroud)

编辑:根据评论中的建议修改代码。实施更改后仍然出现相同的错误

import SwiftUI

struct ContentView: View {

    @EnvironmentObject var shared:SharedObject

    var body: some View {
        NavigationView {
            VStack {
                Button(action: {self.shared.test()}) {Text("Add")}.padding()
                List {
                    ForEach(self.shared.accounts) { account in
                        NavigationLink(destination:
                            List {
                                ForEach(self.shared.journalLines.filter({$0.accountID == account.id})) { line in
                                    HStack {
                                        Text(line.payee)
                                        Spacer()
                                        Text("$ \(line.amount, specifier: "%.2f")")
                                    }
                                }
                            }.frame(maxWidth: .infinity, maxHeight: .infinity)
                        ) {
                            HStack {
                                Text(account.name)
                                Spacer()
                                Text("$ \(self.shared.journalLines.filter({$0.accountID == account.id}).map({$0.amount}).reduce(0){$0+$1}, specifier: "%.2f")")
                            }
                        }
                    }
                }
                .frame(width: 200)
            }
        }.frame(maxWidth: .infinity, maxHeight: .infinity)
    }

}

class Account:Identifiable {
    init(id:Int,name:String) {
        self.id=id
        self.name=name
    }
    var id:Int
    var name:String
}
class JournalLine:Identifiable {
    init(payee:String,accountID:Int,amount:Double) {
        self.payee=payee
        self.accountID=accountID
        self.amount=amount
    }
    let id:UUID = UUID()
    var payee:String
    var accountID:Int
    var amount:Double
}

class SharedObject:ObservableObject {
    @Published var accounts:[Account] = []
    @Published var journalLines:[JournalLine] = []

    init() {

        self.accounts.append(Account(id: 1, name: "Account#1"))
        self.accounts.append(Account(id: 2, name: "Account#2"))
        self.accounts.append(Account(id: 3, name: "Account#3"))

        self.journalLines.append(JournalLine(payee: "Payee1", accountID: 1, amount: 100))
        self.journalLines.append(JournalLine(payee: "Payee2", accountID: 1, amount: 200))
        self.journalLines.append(JournalLine(payee: "Payee3", accountID: 2, amount: 300))
        self.journalLines.append(JournalLine(payee: "Payee4", accountID: 2, amount: 400))
        self.journalLines.append(JournalLine(payee: "Payee5", accountID: 3, amount: 500))
        self.journalLines.append(JournalLine(payee: "Payee6", accountID: 3, amount: 600))

    }

    func test() {
        self.journalLines.append(JournalLine(payee: "Payee10", accountID: 1, amount: 100))
        self.journalLines.append(JournalLine(payee: "Payee10", accountID: 2, amount: 100))
        self.journalLines.append(JournalLine(payee: "Payee10", accountID: 3, amount: 100))
    }

}
Run Code Online (Sandbox Code Playgroud)

Tej*_*ina 7

这是我在 SwiftUI 的 List/LazyVStack 中观察到的一个常见问题。

我使用了一个简单的技巧来解决这个问题。将 UniqueID 分配给您想要动态更新的视图。

列表示例:

List(items, id: \.self) {
  Text($0.name)
}
 .id(UUID())
Run Code Online (Sandbox Code Playgroud)

LazyVStack 的示例:

LazyVStack {
  ForEach(items, id: \.self) { item in 
     Text(item.name)
  }
}
 .id(UUID())
Run Code Online (Sandbox Code Playgroud)

  • 简单地使用 UUID() 会使列表不稳定,它会在某些特定情况下重绘。例如,当我在删除之前显示一个确认对话框时,该对话框会出现两次,这很奇怪。我的解决方案是使项目可哈希,并在计算哈希时考虑所有属性。然后使用 `\.self` 作为 List 元素的 id 就足够了。例如 `ForEach(items, id: \.self)` (3认同)

小智 3

回答

我不知道这是一个错误还是功能,但列表是静态的,这意味着不寻找更新。如果您想跟踪更新,目前常见的解决方法是将 ForEach 放置在 List 中。这样,每次更新@Published对象时,您的视图都会更新。

以下是如何执行列表+ foreach 部分https://www.hackingwithswift.com/quick-start/swiftui/building-a-menu-using-list