¡mundo! Quiero asegurarme de que los similares se coloquen solo en una tarjeta, y no en todas a la vez. Escribí la lógica de pintar el me gusta y hacer clic en él. Pero esto se aplica a todas las tarjetas, no solo a la que hice clic en Me gusta.
¿Cómo puedo cambiar la lógica para que el Me gusta se coloque solo en la tarjeta en la que hice clic?
Debajo del código CellView() hay una celda con una tarjeta, que luego uso en otra vista
struct CellView: View{ //MARK: - PROPERTIES @AppStorage("isLiked") var isLiked: Bool = false var data: Model //MARK: - BODY var body: some View{ VStack{ VStack{ AnimatedImage(url: URL(string: data.photo)!) .resizable() .aspectRatio(contentMode: .fit) .overlay(RoundedRectangle(cornerRadius: 10).stroke(Color("GrayWhite"), lineWidth: 0.5) ) }.background(Color.white) .cornerRadius(10) VStack{ HStack{ Text("\(data.price) ₽") .font(.system(size: 16, weight: .bold)) .foregroundColor(Color("BlackWhite")) Spacer() Button(action: { self.isLiked.toggle() }, label: { Image(systemName: isLiked ? "heart.fill" : "heart") .frame(width: 22, height: 22) .foregroundColor(isLiked ? .red : .black) }) }.padding(.bottom, 1) HStack{ Text(data.company) .font(.system(size: 14, weight: .bold)) .lineLimit(1) .foregroundColor(Color("BlackWhite")) Spacer() } HStack{ Text(data.name) .font(.system(size: 14, weight: .bold)) .lineLimit(1) .foregroundColor(.gray) Spacer() } } .padding(.horizontal, 2) } .padding(5) } }Gracias de antemano por su ayuda
Cada celda debe identificarse para que Swift sepa qué le gusta.
import SwiftUI struct CardData: Identifiable { var id: String = UUID().uuidString // add the rest of the data associated with the cards, company, etc. }Y si queremos seguir la estructura MVVM, cree un archivo separado para las funciones (cuando marcamos como y diferente de cada tarjeta).
class CellViewModel: ObservableObject { @Published var card: CardData! @Published var isLiked = false func like() { isLiked.toggle() } }Ahora todos nuestros CellViews pueden gustar por separado (eliminé algunas de sus entradas, para probar)
struct CellView: View{ //MARK: - PROPERTIES @ObservedObject var cellViewModel: CellViewModel = CellViewModel() init(cardData: CardData) { self.cellViewModel.card = cardData } //MARK: - BODY var body: some View{ VStack{ VStack{ Image(systemName: "circle") .resizable() .aspectRatio(contentMode: .fit) .overlay(RoundedRectangle(cornerRadius: 10).stroke(Color("GrayWhite"), lineWidth: 0.5) ) }.background(Color.white) .cornerRadius(10) VStack{ HStack{ Text(" ₽") .font(.system(size: 16, weight: .bold)) .foregroundColor(Color("BlackWhite")) Spacer() Button(action: { cellViewModel.like() }, label: { Image(systemName: cellViewModel.isLiked ? "heart.fill" : "heart") .frame(width: 22, height: 22) .foregroundColor(cellViewModel.isLiked ? .red : .black) }) }.padding(.bottom, 1) HStack{ Text("company") .font(.system(size: 14, weight: .bold)) .lineLimit(1) .foregroundColor(Color("BlackWhite")) Spacer() } HStack{ Text("name") .font(.system(size: 14, weight: .bold)) .lineLimit(1) .foregroundColor(.gray) Spacer() } } .padding(.horizontal, 2) } .padding(5) } }Probando el gusto por separado:
struct ContentView: View { var body: some View { VStack { CellView(cardData: CardData()) CellView(cardData: CardData()) CellView(cardData: CardData()) CellView(cardData: CardData()) } } }