Listview 从coredata SwiftUI中删除多行
我试图在编辑模式下使用选择同时删除多个核心数据。但是,该行不会被删除。我不确定是什么问题 我尝试在ForEach上添加Listview 从coredata SwiftUI中删除多行,listview,core-data,swiftui,selection,Listview,Core Data,Swiftui,Selection,我试图在编辑模式下使用选择同时删除多个核心数据。但是,该行不会被删除。我不确定是什么问题 我尝试在ForEach上添加id:\.id或id:\.self(注释) 我的代码有什么问题?有没有其他方法可以删除多行 struct TempNoteView: View { @Environment(\.managedObjectContext) private var viewContext @FetchRequest(sortDescriptors: [],animation: .d
id:\.id
或id:\.self
(注释)
我的代码有什么问题?有没有其他方法可以删除多行
struct TempNoteView: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(sortDescriptors: [],animation: .default)
private var notes: FetchedResults<Note>
@State var editMode: EditMode = .inactive
@State var selection = Set<UUID>()
var body: some View {
List(selection: $selection) {
ForEach(notes) { note in
NavigationLink(destination: NoteView(note: note)) {
Text(note.name ?? "Untitled")
Spacer()
Button(action: {
if note.pin == false {
note.pin = true
saveContext()
} else {
note.pin = false
saveContext()
}
}) {
if note.pin {
Image("pinned").resizable().frame(width: 23, height: 23, alignment: .center)
.padding(.trailing, 10)
} else {
Image("not-pinned").resizable().frame(width: 23, height: 23, alignment: .center)
.padding(.trailing, 10)
}
}.buttonStyle(PlainButtonStyle())
}
.padding()
.listRowInsets(EdgeInsets())
// .listRowBackground(Color.gray.opacity(0.1))
.background(Color.yellow.opacity(0.3))
.clipShape(RoundedRectangle(cornerRadius: 10))
.listStyle(SidebarListStyle())
.padding(.init(top: 10, leading: 0, bottom: 10, trailing: 0))
}.onDelete(perform: deleteNotes)
}
.environment(\.editMode, self.$editMode)
.navigationTitle("Notes")
.navigationBarItems(
leading:
HStack {
editButton
deleteButton
},
trailing: Button(action: {addNote()}, label: {
Text("Add Note")
Image("plus")
})).onAppear(){
UITableViewCell.appearance().selectionStyle = .none
}
}
private var editButton: some View {
if editMode == .inactive {
return Button(action: {
self.editMode = .active
self.selection = Set<UUID>()
}) {
Text("Edit")
}
}
else {
return Button(action: {
self.editMode = .inactive
self.selection = Set<UUID>()
}) {
Text("Done")
}
}
}
private var deleteButton: some View {
if editMode == .inactive {
return Button(action: {}) {
Image("")
}
} else {
return Button(action: deleteAllNotes) {
Image(systemName: "trash")
}
}
}
private func deleteAllNotes() {
for id in selection {
if let index = notes.lastIndex(where:{ $0.id == id }) {
viewContext.delete(notes[index])
}
}
selection = Set<UUID>()
}
func saveContext() {
do {
try viewContext.save()
} catch {
let error = error as NSError
fatalError("Unresolved Error: \(error)")
}
}
private func deleteNotes(offsets: IndexSet) {
withAnimation {
offsets.map { notes[$0] }.forEach(viewContext.delete)
saveContext()
}
}
private func addNote() {
withAnimation {
let newNote = Note(context: viewContext)
newNote.id = UUID()
newNote.name = "New Note"
newNote.createdAt = Date()
newNote.pin = false
saveContext()
}
}
}
struct TempNoteView:View{
@环境(\.managedObjectContext)私有变量viewContext
@FetchRequest(sortDescriptors:[],动画:。默认值)
私有变量注释:FetchedResults
@状态变量editMode:editMode=.inactive
@State var selection=Set()
var body:一些观点{
列表(选择:$selection){
ForEach(注释){注
导航链接(目的地:注释视图(注释:注释)){
文本(注:名称??“无标题”)
垫片()
按钮(操作:{
如果note.pin==false{
note.pin=true
saveContext()
}否则{
note.pin=false
saveContext()
}
}) {
如果note.pin{
图像(“固定”)。可调整大小()。帧(宽度:23,高度:23,对齐:。中心)
.padding(.training,10)
}否则{
图像(“未固定”)。可调整大小()
.padding(.training,10)
}
}.buttonStyle(PlainButtonStyle())
}
.padding()
.listRowInsets(EdgeInsets())
//.listRowBackground(颜色、灰色、不透明度(0.1))
.背景(颜色.黄色.不透明度(0.3))
.clipShape(圆角半径:10))
.listStyle(SidebarListStyle())
.padding(.init(顶部:10,前导:0,底部:10,尾随:0))
}.onDelete(执行:删除注释)
}
.environment(\.editMode,self.$editMode)
.navigationTitle(“注释”)
.航海术语(
领先:
HStack{
编辑按钮
删除按钮
},
尾部:按钮(操作:{addNote()},标签:{
文本(“添加注释”)
图像(“加”)
})).onAppear(){
UITableViewCell.appearance().selectionStyle=.none
}
}
私有var编辑按钮:一些视图{
如果editMode==.inactive{
返回按钮(操作:{
self.editMode=.active
self.selection=Set()
}) {
文本(“编辑”)
}
}
否则{
返回按钮(操作:{
self.editMode=.inactive
self.selection=Set()
}) {
文本(“完成”)
}
}
}
private-var-deleteButton:一些视图{
如果editMode==.inactive{
返回按钮(操作:{}){
图像(“”)
}
}否则{
返回按钮(操作:deleteAllNotes){
图像(系统名称:“垃圾”)
}
}
}
私有函数deleteAllNotes(){
用于选择中的id{
如果let index=notes.lastIndex(其中:{$0.id==id}){
viewContext.delete(注释[索引])
}
}
选择=设置()
}
func saveContext(){
做{
请尝试viewContext.save()
}抓住{
let error=错误为N错误
fatalError(“未解决的错误:\(错误)”)
}
}
专用函数deleteNotes(偏移量:索引集){
动画片{
offset.map{notes[$0]}.forEach(viewContext.delete)
saveContext()
}
}
私有函数addNote(){
动画片{
让newNote=Note(上下文:viewContext)
newNote.id=UUID()
newNote.name=“新注释”
newNote.createdAt=Date()
newNote.pin=false
saveContext()
}
}
}
将您的ForEach更改为
ForEach(notes, id:\.self) { note in
然后将集合更改为“将笔记作为类型”
@State var selection = Set<Notes>()
@State var selection=Set()
然后在deleteAllNotes(我将其重命名为deleteAllSelectedNotes)中删除所有选中的注释
private func deleteAllNotes() {
for item in selection {
viewContext.delete(item)
}
selection = Set<Note>()
}
private func deleteAllNotes(){
用于选择中的项目{
viewContext.delete(项目)
}
选择=设置()
}