【问题标题】:Show view only if items are selected in list using EditButton and multiple selection in SwiftUI仅当使用 EditButton 在列表中选择项目并在 SwiftUI 中选择多项时才显示视图
【发布时间】:2021-12-11 17:56:24
【问题描述】:

以下代码可让您从列表中选择多个项目,并在点击按钮时清除选择列表。我希望能够做的是不显示Deselect All 按钮,除非在列表中选择了项目,或者至少在编辑模式下。

有没有办法只在选择项目时才显示视图?或者至少只在编辑模式下?

struct ContentView: View {
    @State private var itemSelection = Set<String>()

    let names = [ "Orange", "Apple", "Grape", "Watermelon"]

    var body: some View {
        NavigationView {
            VStack{
                List(names, id: \.self, selection: $itemSelection) { name in
                    Text(name)
                }
                .navigationTitle("Item List")
                .toolbar {
                    EditButton()
                }
                
                Button("Deselect All"){
                    print("Items: \(itemSelection)")
                    itemSelection.removeAll()
                }
            }
        }
    }
}

【问题讨论】:

  • 我的回答中还有第二种方法。

标签: swift swiftui


【解决方案1】:

这里有一个适合你的方法:

struct ContentView: View {
    
    @State private var itemSelection = Set<String>()

    let names = [ "Orange", "Apple", "Grape", "Watermelon"]

    var body: some View {
        NavigationView {
            VStack{
                List(names, id: \.self, selection: $itemSelection) { name in
                    Text(name)
                }
                .navigationTitle("Item List")
                .toolbar {
                    EditButton()
                }
                
                if !itemSelection.isEmpty {
                    
                    Button("Deselect All"){
                        print("Items: \(itemSelection)")
                        itemSelection.removeAll()
                    }
                    .transition(AnyTransition.move(edge: .bottom))
                    
                }

            }
            .animation(.default, value: itemSelection.isEmpty)
        }
    }
}

这是第二种方式,如果你选择了一些项目,然后你没有取消选择项目,那么应用程序会认为你不想再有任何选择。如果您即使在结束编辑后也想进行选择,那么第一个答案是给您的,而不是第二个答案:

PS:我发现了一个奇怪的错误,当我们有 NavigationView 时,editMode 不可读或不可用,因此我为编辑按钮定义了一个独立视图。

struct ContentView: View {

    @State private var names: [String] = ["Orange", "Apple", "Grape", "Watermelon"]
    @State private var itemSelection = Set<String>()
    
    var body: some View {
        NavigationView {
            VStack{
                List(names, id: \.self, selection: $itemSelection) { name in
                    Text(name)
                }
                .navigationTitle("Item List")
                .toolbar { EditModeView(itemSelection: $itemSelection) }
                
                if !itemSelection.isEmpty {
                    
                    Button("Deselect All"){
                        print("Items: \(itemSelection)")
                        itemSelection.removeAll()
                    }
                    .transition(AnyTransition.move(edge: .bottom))
                }
            }
            .animation(.default, value: itemSelection.isEmpty)
        }
    }
}

struct EditModeView: View {
    
    @Environment(\.editMode) var editMode
    @Binding var itemSelection: Set<String>
    
    var body: some View {
        EditButton()
            .onChange(of: editMode?.wrappedValue.isEditing, perform: { newValue in
                
                if let unwrappedNewValue: Bool = newValue, (!unwrappedNewValue) {
                    itemSelection.removeAll()
                }
                
            })
    }
}

【讨论】:

  • 非常感谢您提供的好例子。 - 我真的很喜欢你添加的显示/隐藏按钮的动画。
猜你喜欢
  • 2013-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-07
  • 2011-09-07
相关资源
最近更新 更多