微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SwiftUI ForEach中的多个选择

如何解决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))
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。