Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
La lógica de hacer clic en Me gusta en SwiftUI

¡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

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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()) } } }

ingrese la descripción de la imagen aquí

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda