我试图使用@EnvironmentObject 在视图之间传递数据,但不知何故我的 UI 没有更新。我可以看到我可以在打印时在数组中获取新数据,但 UI 没有更新我在哪里出错了。
这是我更改 environmentObject 数组的地方
struct SingleSelectionRow : View {
@Binding var selectSingle : Int
@Binding var selectedFilter : String?
var title : String
var isSelected : Bool
var childrenList : [FilterChildren]
@State var filterDetail : SortAndFilterData
@Binding var singleSelectedFilters : [String]
@State var isExpanded : Bool = false
var action : () -> Void
@State var onClick : Bool = false
@EnvironmentObject var childSelections : ProductListViewModel
var body : some View {
if selectSingle == 1 {
if childrenList.isEmpty == true {
Button(action: {
self.action()
}) {
Text(self.title.uppercased())
.font(.custom("DM Sans",size:15))
.foregroundColor(isSelected ? Color(red: 10 / 255, green: 207 / 255, blue: 131 / 255) : Color.black)
}
}
if childrenList.isEmpty == false {
DisclosureGroup(
isExpanded: $isExpanded,
content: {
ForEach(childrenList, id:\.id) { item in
Button(action: {
self.action()
self.isExpanded = false
}) {
Text(item.name ?? "")
.font(.custom("DM Sans",size:15))
.frame(maxWidth: .infinity, alignment: .leading)
.foregroundColor(childSelections.selectionFilters.contains(item.name ?? "") ? Color(red: 10 / 255, green: 207 / 255, blue: 131 / 255) : Color.black)
.contentShape(Rectangle())
.onTapGesture {
singleSelectedChildRows(item: item)
}
}
}
},
label: {
Text(self.title.uppercased())
.foregroundColor(isSelected ? Color(red: 10 / 255, green: 207 / 255, blue: 131 / 255) : Color.black)
}
)
.contentShape(Rectangle())
.onTapGesture {
withAnimation(.easeOut) {
isExpanded.toggle()
self.action()
}
}
}
}
}
func singleSelectedChildRows(item : FilterChildren ) {
DispatchQueue.main.async {
if childSelections.selectionFilters.contains(item.name ?? "") {
self.childSelections.selectionFilters.removeAll(where: { $0 == item.name ?? ""})
} else {
self.childSelections.selectionFilters.removeAll()
self.childSelections.selectionFilters.append(item.name ?? "")
print("Selected Filter is : \(childSelections.selectionFilters)")
}
}
}
}
这是我想使用更改数组的地方。
@EnvironmentObject var childSelections : ProductListViewModel
var body: some View {
NavigationView {
ZStack{
List {
ForEach(filterListArray.filter{ $0.id != "sort" && $0.id != "stock"}, id:\.id) { data in
NavigationLink(destination : FilterDetailView(filterDetailList: data.sortAndFilterList ?? [], navigationBarTitle: data.name ?? "", multiSelectedFilters: $filterState.multiSelections, singleSelectedFilters: $filterState.singleSelections, singleSelectedChildFilters: filterState.singleSelections, isApply: $filterState.isApply, selectSingle: data.selectSingle ?? 1)
){
VStack(alignment: .leading,spacing:10){
FilterCell(filterType: data)
HStack{
ForEach((filterState.multiSelections + filterState.singleSelections + childSelections.selectionFilters), id:\.self ) { item in
if filterState.isApply == true {
SelectedFiltersRow(filterSelection: item, sortAndFilterData: data)
}
}
}
}
}
}
这是我的 viewModel ;
class ProductListViewModel : ObservableObject {
var productList = PassthroughSubject<ProductListResponse,Never>()
var cancellable = Set<AnyCancellable>()
var storeResponse = InfoResponse()
@Published var selectionFilters : [String] = []
var currentPage = 1
var perPage = 12
func getProductListResponse(typeId : String , type : String, sortId : String,mainItemId: String) {
let tsoftFilter = """
[{"key" : "\(type)", "value" : "\(typeId)"}]
"""
print("tsoft filter is \(tsoftFilter)")
let serviceParams = [
"store_id" : Config.storeId,
"main_page_item_id" :"\(mainItemId)",
"page" : currentPage,
"per_page" : perPage,
"tsoft_filters" : "\(tsoftFilter)",
"sort" : Int(sortId)
] as [String : AnyObject]
getProductList(params: serviceParams).sink { (error) in
print("ERROR İS -> \(error)")
} receiveValue: { (ProductListResponse) in
self.productList.send(ProductListResponse)
}.store(in: &cancellable)
}
}