SwiftUI列表未更新

时间:2020-03-08 15:20:06

标签: macos swiftui

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

我尝试将列表中的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))
    }

}

edit:修改后的代码,带有注释的建议。实施更改后仍然会遇到相同的错误

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))
    }

}

2 个答案:

答案 0 :(得分:1)

答案

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

这是列表+ foreach部分https://www.hackingwithswift.com/quick-start/swiftui/building-a-menu-using-list

的处理方法

答案 1 :(得分:1)

列表需要进行重要的更改,例如删除,附加或替换,否则将不会更新视图。当您替换列表中的单个项目时,唯一ID必须更改,否则,视图更新将不会发生。所以,

let listindex = bildList.firstIndex(of: listitem)
bildList[listindex!].itemname = "new name"
bildList[listindex!].id = UUID()

更改当前项目的ID后,将刷新视图。