簡體   English   中英

SwiftUI ForEach 中的多項選擇

[英]Multiple selections in SwiftUI ForEach

我正在嘗試選擇 ForEach 視圖的多個視圖,但它沒有反映我的選擇。 我找到了一個解決方案,但我似乎沒有實時工作,它只更改數據,而不是視圖。我返回並重新打開視圖,它反映了更改。 另外,一旦我選擇了多個選項,我需要將它發送到服務器,我怎么知道我的選擇是什么? 我想我應該再次將 userProfile.domainsOfInterest 映射到 viewModel.domains。 這是我的屏幕代碼:

struct DomainsOfInterestView: View {
    @EnvironmentObject var userProfile: UserProfile

    @ObservedObject var viewModel: DomainsViewModel = DomainsViewModel()
        
    var body: some View {
            VStack(alignment: .center) {
                HStack {
                    Text("Choose domains of interest for your profile")
                        .foregroundColor(Color.orGrayColor)
                        .font(.custom("SFProDisplay-Regular", size: 16))
                    Spacer()
                }.padding(.bottom, 16)
                VStack{
                    ScrollView {
                        ForEach(viewModel.domains, id: \.self) {item in
                            DomainOfInterestElement(interestDomain: item, isSelected: self.userProfile.domainsOfInterest.contains(item)) {
                                if self.userProfile.domainsOfInterest.contains(item) {
                                    self.userProfile.domainsOfInterest.removeAll(where: { $0 == item })
                                }
                                else {
                                    self.userProfile.domainsOfInterest.append(item)
                                }
                            }
                        }
                    }
                    .padding([.top, . bottom], 15)
                }
                .background(Color.white)
                .cornerRadius(8)
                Spacer()
                OrangeButton(action: {

                }) {
                    Text("Save")
                }.padding([.leading, .trailing, .top], 30)
                    .padding(.bottom, 24)
            }.padding([.leading, .trailing], 12)
            .navigationBarTitle("Domains of interest")
            .background(Color.orBackgroundGrayColor.edgesIgnoringSafeArea(.all))
    }
}

ForEach 的觀點:

struct DomainOfInterestElement: View {
    var interestDomain: InterestDomain
    var isSelected: Bool
    var action: () -> Void
    
    var body: some View {
        Button(action: {
            self.action()
        }) {
            VStack {
                HStack {
                    checkBoxView()
                        .frame(width: 36, height: 36)
                    textView().font(.custom("SFProDisplay-Regular", size: 16))
                    Spacer()
                }
            }
        }
    }
    
    func checkBoxView() -> Image {
        switch isSelected {
        case true:
            return Image(uiImage: #imageLiteral(resourceName: "check-box-active-2-1")).renderingMode(.original)
        case false:
            return Image(uiImage: #imageLiteral(resourceName: "check-box-active-1")).renderingMode(.original)
        }
    }
    func textView() -> Text {
        switch isSelected {
        case true:
            return Text(interestDomain.name)
                .foregroundColor(.black)
        case false:
            return Text(interestDomain.name)
                .foregroundColor(Color.orGrayColor)
        }
    }
}

我希望下面的代碼可以幫助你,只是簡單的列表磁帶的屬性更改,然后收集所有選定的真實項目來處理它們,

List(list){item in
Button(action: {
      if let index =  self.list.firstIndex(where: {$0.id == item.id }){
         self.list[index].isSelected =  !item.isSelected
                        }
  }) {
    Image(systemName: item.isSelected ? "checkmark.circle" : "circle")
       .foregroundColor(item.isSelected ? Color.black : Color.gray)
       .font(Font.title.weight(.light))
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM