Zde je kód, pokud nechcete pochopit
struct cartView: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(
sortDescriptors: [], animation: .default) private var products: FetchedResults<Prod>
let columns = [GridItem(.flexible()), GridItem(.flexible())]
@State var indx = 0
var body: some View {
NavigationView {
ScrollView {
VStack {
LazyVGrid(columns: columns) {
ForEach(products, id: \.self) {prod in
let prodItems = Product(name: prod.name ?? "Undefined", price: prod.price ?? "Undefined", type: "type", brand: prod.brand ?? "Undefined", images: [prod.image!,""])
ZStack {
Cells(product: prodItems)
// I want the button below delete the item of the
// button pressed with the function below
Button(action: {}) {
Image(systemName: "xmark.bin.circle")
.resizable()
.frame(width: 30, height: 30)
.foregroundColor(.red)
.background(.black)
.clipShape(RoundedRectangle(cornerRadius: 20))
.offset(x: 60, y: 45)
}
}
}
}
}
}
.navigationTitle("Cart")
}
}
private func deleteItems(offsets: IndexSet) {
withAnimation {
offsets.map { products[$0] }.forEach(viewContext.delete)
do {
try viewContext.save()
} catch {
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
}
}
takže otázka zde je tlačítko, tady v forEach smyčky, odstranit položky, které uživatel stiskl tlačítko a vím, jak na to, ale co nevím je, jak se dostat na index položky a předat ji ve funkci.