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)
            }
        }
Ruzev
  • 11
  • 2

1 Answers1

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

Ruzev
  • 11
  • 2