1

Exemple render

Hello I am looking for help, here is my problem: I want to add buttons on the elements of my lazygrid, the problem is that I cannot change the value in the foreach of the elements on which I iterate. The problem is that I can't use @Binding variables. If you have any advice or a solution I would appreciate it.

Here is my code :

  LazyVGrid(columns: columns) {
            if let interest = profilData.detailProfil.interests {
                ForEach(interest) { i in
                    VStack{
                        Image(systemName: i.icon)
                            .font(.system(size: 25, weight: .bold))
                            .foregroundColor(Color("TopBackGroundColor"))
                            .padding()
                            .background(.black)
                            .clipShape(Circle())
                            .overlay(alignment: .bottomTrailing) {
                                Button(action: {
                                    // Where i want switch bool value enable in interests
                                }, label : {
                                    Image(systemName: i.enable ? "checkmark.circle.fill" : "x.circle.fill")
                                        .font(.system(size: 25, weight: .bold))
                                        .foregroundColor(i.enable ? Color.green : Color.red)
                                })
                                .padding(.trailing, -5)
                                .padding(.bottom, -5)
                            }
                        Text(i.name)
                            .font(.title2)
                    }
                }
            } else {
                HStack(alignment: .center){
                    Spacer()
                    Text(LocalizedStringKey("nointerst.id"))
                        .fontWeight(.bold)
                        .foregroundColor(Color("TextColor"))
                    Spacer()
                }
                .padding()
                .background(.gray.opacity(0.2))
                .cornerRadius(20)
            }
        }
3
  • Put everything in the content of the for each in a subview Commented Jun 3, 2022 at 13:34
  • Do you use a @ObservableObject for your data? Your views would then automatically react to reflect the changes in your data. Commented Jun 3, 2022 at 13:55
  • The problem is that the i is considered as let. This message shows "Cannot assign to property: 'i' is a 'let' constant" Commented Jun 4, 2022 at 14:10

1 Answer 1

0

I found a solution thanks to this post. So I managed to change the value of my object in my foreach. The only thing to do that was not specified is that it is necessary to use a hashable struct.

https://stackoverflow.com/a/62547311/7951080

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.