gpt4 book ai didi

数据更改后 SwiftUI 列表未更新。 (需要切换 View )

转载 作者:行者123 更新时间:2023-12-04 15:11:45 29 4
gpt4 key购买 nike

我正在尝试更改 SwiftUI 列表以在点击列表中的复选框按钮后进行更新。当我点击列表行复选框按钮时,我调用一个函数来将直接行设置为选中,哪个 ID 小于所选行。我可以将 ArrayList 修改为 selected = 0 到 selected = 1。但是由于我的列表是 Published var,它应该将更改发送到我的 ListView 。但事实并非如此。

这是我所做的:

// View 模型

import Foundation
import Combine

class BillingViewModel: ObservableObject {

@Published var invoiceList = [InvoiceModel](){
didSet {
self.didChange.send(true)
}
}

@Published var shouldShow = true

let didChange = PassthroughSubject<Bool, Never>()

init() {
setValues()
}

func setValues() {
for i in 0...10 {
invoiceList.append(InvoiceModel(ispID: 100+i, selected: 0, invoiceNo: "Invoice No: \(100+i)"))
}
}

func getCombinedBalance(ispID: Int) {
DispatchQueue.main.async {

if let row = self.invoiceList.firstIndex(where: {$0.ispID == ispID}) {
self.changeSelection(row: row)
}

}
}

func changeSelection(row: Int) {
if invoiceList[row].selected == 0 {

let selectedRows = invoiceList.map({ $0.ispID ?? 0 <= invoiceList[row].ispID ?? 0 })

print(selectedRows)

for index in 0..<invoiceList.count {
if selectedRows[index] {
invoiceList[index].selected = 1
} else {
invoiceList[index].selected = 0
}
}

} else {

let selectedRows = invoiceList.map({ $0.ispID ?? 0 <= invoiceList[row].ispID ?? 0 })

print(selectedRows)

for index in 0..<invoiceList.count {
if selectedRows[index] {
invoiceList[index].selected = 1

} else {
invoiceList[index].selected = 0
}
}
}
}

}

// ListView

import SwiftUI

struct ContentView: View {

@StateObject var billingViewModel = BillingViewModel()
@State var shouldShow = true

var body: some View {
NavigationView {
ZStack {
VStack {
List {
ForEach(billingViewModel.invoiceList) { invoice in
NavigationLink(
destination: InvoiceDetailsView(billingViewModel: billingViewModel)) {
invoiceRowView(billingViewModel: billingViewModel, invoice: invoice)
}
}

}
}
}.navigationBarTitle(Text("Invoice List"))
}

}
}

//发票行 View

import SwiftUI

struct invoiceRowView: View {

@StateObject var billingViewModel: BillingViewModel
@State var invoice: InvoiceModel

var body: some View {
VStack(alignment: .leading) {
HStack {
Button(action: {
if invoice.selected == 0 {
print(invoice)
billingViewModel.getCombinedBalance(ispID: invoice.ispID ?? 0)
} else {
print(invoice)
billingViewModel.getCombinedBalance(ispID: invoice.ispID ?? 0)
}
}, label: {
Image(systemName: invoice.selected == 0 ? "checkmark.circle" : "checkmark.circle.fill")
.resizable()
.frame(width: 32, height: 32, alignment: .center)

}).buttonStyle(PlainButtonStyle())

Text(invoice.invoiceNo ?? "Hello, World!").padding()

Spacer()
}
}
}
}

//数据模型

import Foundation

struct InvoiceModel: Codable, Identifiable {
var id = UUID()
var ispID: Int?
var selected: Int?
var invoiceNo: String?
}

最佳答案

您需要使用绑定(bind)而不是外部注入(inject)状态(设置为值的副本),即

struct invoiceRowView: View {

@StateObject var billingViewModel: BillingViewModel
@Binding var invoice: InvoiceModel

// .. other code

然后在父 View 中注入(inject)绑定(bind)

ForEach(billingViewModel.invoiceList.indices, id: \.self) { index in
NavigationLink(
destination: InvoiceDetailsView(billingViewModel: billingViewModel)) {
invoiceRowView(billingViewModel: billingViewModel, invoice: $billingViewModel.invoiceList[index])
}
}

关于数据更改后 SwiftUI 列表未更新。 (需要切换 View ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65107436/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com