Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!