यहाँ कोड है, तो आप समझ में नहीं आता
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)")
}
}
}
}
तो यहाँ सवाल यह है बटन वहाँ में forEach पाश को हटाने के लिए, आइटम है कि उपयोगकर्ता बटन दबाया और मैं यह कैसे करना है पता है, लेकिन मैं क्या नहीं पता है कि कैसे प्राप्त करने के लिए सूचकांक के आइटम और इसे पारित एक समारोह है ।