【问题标题】:Select and Delete Core Data entities in SwiftUI List View on macOS在 macOS 上的 SwiftUI 列表视图中选择和删除核心数据实体
【发布时间】:2021-05-17 15:04:10
【问题描述】:

我是 SwiftUI 的新手,但取得了合理的进展。我正在使用最新版本的 Xcode 12.4 并运行 BigSur 11.2.1。我正处于想要使用核心数据的阶段,但遇到了无法解决的问题。

当我创建基本 Xcode 项目时,我选择 App 和 macOS 作为模板 然后我选择 界面 - SwiftUI, 生命周期 - SwiftUI 应用程序, 语言 - 斯威夫特 并选择 使用核心数据

创建了一个新项目,并且构建和运行没有任何问题。 在出现的窗口中,我只需单击顶部栏上的 + 按钮即可添加新项目(日期戳)。到目前为止,一切都很好。这都是香草苹果代码。

我卡住的地方:- List - ContentView 中的 ForEach 视图不允许通过单击选择任何实体(项目),因此我找不到删除条目的方法。

如果我用一组文本项替换实体,那么我可以选择它们并删除它们 通过使用@State var selectKeeper = Set() 有一个选择:列表视图中的 $selectKeeper。

有人可以解释一下怎么做吗?

这是内容视图的原始代码。

import SwiftUI
import CoreData

struct ContentView: View {
    @Environment(\.managedObjectContext) private var viewContext

    @FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],
        animation: .default)
    private var items: FetchedResults<Item>

    var body: some View {
        List {
            ForEach(items) { item in
                Text("Item at \(item.timestamp!, formatter: itemFormatter)")
            }
            .onDelete(perform: deleteItems)
        }
        .toolbar {
            Button(action: addItem) {
                Label("Add Item", systemImage: "plus")
            }
        }
    }

    private func addItem() {
        withAnimation {
            let newItem = Item(context: viewContext)
            newItem.timestamp = Date()

            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)")
            }
        }
    }

    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)")
            }
        }
    }
}

private let itemFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateStyle = .short
    formatter.timeStyle = .medium
    return formatter
}()

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
    }
}

【问题讨论】:

    标签: macos core-data swiftui xcode12


    【解决方案1】:

    您应该添加 EditButton() 并可能将所有这些都包含在 NavitagionView 中可能会给您所需的内容:

    var body: some View {
        NavigationView{
            List {
                ForEach(items) { item in
                    Text("Item at \(item.timestamp!, formatter: itemFormatter)")
                }
                .onDelete(perform: deleteItems)
            }
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) {
                    #if os(iOS)
                    EditButton()
                    #endif
                }
    
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button(action: addItem) {
                    Label("Add Item", systemImage: "plus")
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 感谢您的回复。我确实设法使用 NavigationViews 内置的 .ondelete 函数进行删除。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多