【发布时间】:2020-05-23 17:51:16
【问题描述】:
我在我的应用程序中使用 swiftUI,它使用 coredata 来存储 FoodItems。在我的应用程序中,我有一个存储在用户设备上的所有 foodItems 的列表。假设他们应该能够通过长按吃或扔掉来删除任何一个食物项,但是每次我尝试删除任何食物项时,它总是删除列表中的第一个而不是一个我坚持了很久。谁能帮我?这是我的代码的 sn-p:
ForEach(self.storageIndex.foodItemArray, id:\.self) { item in
RefrigeratorItemCell(icon: item.wrappedSymbol, title: item.wrappedName, lastsUntil: self.addDays(days: Int(item.wrappedStaysFreshFor), dateCreated: item.wrappedInStorageSince))
.gesture(LongPressGesture()
.onEnded({ i in
self.showEatActionSheet.toggle()
})
)
//TODO: Make a diffrence between eat all and throw away
.actionSheet(isPresented: self.$showEatActionSheet, content: {
ActionSheet(title: Text("More Options"), message: Text("Chose what to do with this food item"), buttons: [
.default(Text("Eat All"), action: {
self.managedObjectContext.delete(item)
try? self.managedObjectContext.save()
})
,.default(Text("Throw Away"), action: {
self.managedObjectContext.delete(item)
try? self.managedObjectContext.save()
})
,.default(Text("Cancel"))
])
})
}
【问题讨论】:
标签: ios swift core-data swiftui