ScrollView / LazyVGrid 中 SwiftUI 中的 EditButton()

问题描述

SwiftUI(Xcode 12.5 beta 3)中的 EditButton() 似乎存在各种问题。

在我的代码中,一切正常,直到我用 ScrollView 替换 List 并添加了 LazyVGrid。现在,当用户点击 EditButton 时,不会激活 EditMode。

任何解决方法的想法?拥有 2 列是 UI 的要求,虽然我可以使用列表,但我更喜欢 ScrollView 的外观。我尝试了很多东西......将 ForEach 放在一个部分中并将 EditButton 放在标题中,用手动按钮替换它......不幸的是它们似乎都不起作用:-(

非常感谢您的任何想法或任何其他人为解决此问题所做的任何事情。

struct Home: View {
    @Environment(\.managedobjectContext) private var viewContext
    @FetchRequest(entity: Cars.entity(),sortDescriptors: []) var cars: FetchedResults<Cars>
    
    private var columns: [GridItem] = [
            GridItem(.flexible()),GridItem(.flexible())
        ]

    var body: some View {
  
        NavigationView {
            ScrollView {

                if cars.count > 0 {
                    LazyVGrid(
                        columns: columns) {
                
                ForEach(cars) { n in
                    Text("hello")
                }
                .onDelete(perform: deleteCars)
                    }    
                }
                
                else {
                    Text("You have no cars.")
                }   
            } 
            .navigationBarItems(leading: EditButton())       
        }   
    }
    
    func deleteCars(at offsets: IndexSet) {
        for offset in offsets {
            let cars = cars[offset]
            viewContext.delete(cars)
        }
        try? viewContext.save()
    }
    
}

尝试 1

阅读下面 Asperi 的评论后,我将以下内容(如下)添加到 ScrollView 以手动创建按钮、触发 EditMode 和删除项目。现在我在行 deleteCars 上收到一个错误:“Initializer 'init(_:)' requires that 'FetchedResults.Element'(又名 'Cars')符合 'Sequence'”。

看起来我真的很接近,但仍在挣扎 - 谁能帮我完成最后一部分?非常感谢!

@State var isEditing = false

...

ScrollView {

                if cars.count > 0 {
                    LazyVGrid(
                        columns: columns) {
                        ForEach(cars) { n in
                    Text("hello")
                    Button(action : {
                        deleteCars(at: IndexSet(n))
                        print("item deleted")
                    })
                    
                    {Text("\(isEditing ? "Delete me" : "not editing")")}
                }
                .onDelete(perform: deleteCars)
                .environment(\.editMode,.constant(self.isEditing ? EditMode.active : EditMode.inactive)).animation(Animation.spring())
                    }
                }
                else {
                    Text("You have no cars.")
                }
                Button(action: {
                                    self.isEditing.toggle()
                                }) {
                                    Text(isEditing ? "Done" : "Edit")
                                        .frame(width: 80,height: 40)
                                }
            
            }

解决方法

TLDR:手动创建一个 EditButton 并将其直接添加到 ForEach 循环中。确保 ForEach 数组指定了索引。

好的,终于在上面的尝试 1 中找到了一个答案......通过将 .indices 添加到 ForEach 循环中的数组来工作。全文如下:

struct Home: View {
    @Environment(\.managedObjectContext) private var viewContext
    @FetchRequest(entity: Cars.entity(),sortDescriptors: []) var cars: FetchedResults<Cars>

    @State var isEditing = false
    
    private var columns: [GridItem] = [
            GridItem(.flexible()),GridItem(.flexible())
        ]

    var body: some View {
  
        NavigationView {
            VStack{   
            ScrollView {

                if cars.count > 0 {
                    LazyVGrid(
                        columns: columns) {
                    ForEach(cars.indices,id: \.self) { n in
                    Text("hello")
                    Button(action : {
                        deleteCars(at: [n])
                        print("item deleted")
                    })
                    
                    {Text("\(isEditing ? "Delete me" : "not editing")")}
                        }
                }
                .environment(\.editMode,.constant(self.isEditing ? EditMode.active : EditMode.inactive)).animation(Animation.spring())
                    }
                }
                else {
                    Text("You have no cars.")
                }        
            }
            .navigationBarItems(leading: Button(action: {
                self.isEditing.toggle()
            }) {
                Text(isEditing ? "Done" : "Edit")
                    .frame(width: 80,height: 40)
            }
            ) 
        }   
    }
    
    func deleteCars(at offsets: IndexSet) {
        for offset in offsets {
            let cars = cars[offset]
            viewContext.delete(cars)
        }
        try? viewContext.save()
    }
    
}

我相信有很多方法可以优化这一点(代码和问题/答案的编写方式,所以请随时提出建议。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...