【发布时间】:2021-07-07 00:51:51
【问题描述】:
我创建了一个新的带有核心数据的 Mac OS xcode 应用程序,并且正在使用生成的允许您添加项目的默认代码。
但我想改变删除行的方式。使用长按手势触发删除。
查看:
var body: some View {
List {
ForEach(items) { item in
Text("Item at \(item.timestamp!, formatter: itemFormatter)")
}
//.onDelete(perform: deleteItems) Want to change this command to the one below
.onLongPressGesture(minimumDuration: 1.5, perform: deleteItems)
}
.toolbar {
Button(action: addItem) {
Label("Add Item", systemImage: "plus")
}
}
}
删除项目功能
private func deleteItems(offsets: IndexSet) {
withAnimation {
offsets.map { items[$0] }.forEach(viewContext.delete)
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
但我收到以下错误
无法将类型 '(IndexSet) -> ()' 的值转换为预期的参数类型 '() -> Void'
【问题讨论】:
标签: swiftui swiftui-list