Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda