SwiftUI如何创建带有列表选择的LazyVStack

时间:2020-10-24 11:01:39

标签: swift iphone swiftui ios14

我想在List(selection: )中加入类似LazyVStack的东西。

问题是我不知道如何管理将内容拆分为包含在每个元素中的内容。

我尝试做的事情:

public struct LazyVStackSelectionable<SelectionValue, Content> : View where SelectionValue : Hashable, Content : View {

let content: Content
var selection: Binding<Set<SelectionValue>>?

@Environment(\.editMode) var editMode

public init(selection: Binding<Set<SelectionValue>>?, @ViewBuilder content: () -> Content) {
    self.content = content()
    self.selection = selection
}

public var body: some View {
    
    if self.editMode?.wrappedValue == EditMode.active {
        HStack {
            content //here I would like to have something like ForEach (content, id:\.self)
            
            Button(action: {
                //add the UUID to the list of selected item
            }) {
                Image(systemName: "checkmark.circle.fill")
                //Image(systemName: selection?.wrappedValue.contains(<#T##member: Hashable##Hashable#>) ? "checkmark.circle.fill" : "circle")
            }
        }
        
    }
    else {
        content
    }
    
}
}


struct ListView: View {


@State private var editMode: EditMode = .inactive
@State private var selection = Set<UUID>()

@State private var allElements: [MyElement] = [MyElement(id: UUID(), text: "one"),
                                               MyElement(id: UUID(), text: "two" ),
                                               MyElement(id: UUID(), text: "tree" )
]

var body: some View {

    NavigationView {
        VStack {
            Divider()
            Text("LazyVStack")
                .foregroundColor(.red)
            LazyVStack {
                ForEach(allElements, id: \.self) { element in //section data
                    Text(element.text)
                }
            }
            Divider()
            Text("LazyVStackSelectionable")
                .foregroundColor(.red)
            LazyVStackSelectionable(selection: $selection) {
                ForEach(allElements, id: \.self) { element in //section data
                    Text(element.text)
                }
            }
            Divider()
        }
        .environment(\.editMode, self.$editMode)
        .navigationBarTitle(Text("LIST"), displayMode: .inline)
        .navigationBarItems(//EDIT
            trailing:
            Group {
                HStack (spacing: 15) {
                    self.editButton
                    self.delInfoButton
                    .contentShape(Rectangle())
                }
            }
        )
    }
    
}



//MARK: EDIT MODE
private func deleteItems() {
    
    DispatchQueue.global(qos: .userInteractive).async {
        Thread.current.name = #function

        selection.forEach{ idToRemove in
            if let index = allElements.firstIndex(where: { $0.id == idToRemove }) {
                allElements.remove(at: index)
            }
        }
    }
}

private var editButton: some View {
    Button(action: {
        self.editMode.toggle()
        self.selection = Set<UUID>()
    }) {
        Text(self.editMode.title)
    }
}

private var delInfoButton: some View {
            
    if editMode == .inactive {
        return Button(action: {}) {
            Image(systemName: "square.and.arrow.up")
        }
    } else {
        return Button(action: deleteItems) {
            Image(systemName: "trash")
        }
    }
}

}



struct ListView_Previews: PreviewProvider {
    static var previews: some View {
        ListView()
    }
}

编辑=。无效

enter image description here

编辑=。活动

enter image description here


更新


使用Asperi的解决方案,我失去了LazyVStack的礼节,如果未显示(并且也无法滚动,则所有行也将被加载:

enter image description here

struct SampleRow: View {
    let number: Int

    var body: some View {
        Text("Sel Row \(number)")
    }

    init(_ number: Int) {
        print("Loading LazySampleRow row \(number)")
        self.number = number
    }
}
struct LazySampleRow: View {
    let number: Int

    var body: some View {
        Text("LVS element \(number)")
    }

    init(_ number: Int) {
        print("Loading LazyVStack row \(number)")
        self.number = number
    }
}

var aLotOfElements: [MyElement] {
    var temp: [MyElement] = []
    for i in 1..<200 {
        temp.append(MyElement(id: UUID(), number: i))
    }
    return temp
}

struct ContentView: View {
    
    
    @State private var editMode: EditMode = .inactive
    @State private var selection = Set<UUID>()
    
    @State private var allElements: [MyElement] = aLotOfElements//[MyElement(id: UUID(), number: 1)]
    
    var body: some View {

        NavigationView {
            HStack {
                VStack {
                    Text("LazyVStack")
                    .foregroundColor(.red)
                    ScrollView {
                        LazyVStack (alignment: .leading) {
                            ForEach(allElements, id: \.self) { element in //section data
                                LazySampleRow(element.number)
                            }
                        }
                    }
                }
                
                Divider()
                VStack {
                    LazyVStack (alignment: .leading) {
                        Divider()
                        Text("LazyVStackSelectionable")
                            .foregroundColor(.red)
                        LazyVStackSelectionable(allElements, selection: $selection) { element in
                            SampleRow(element.number)
                        }
                        Divider()
                    }
                }
            }
            .environment(\.editMode, self.$editMode)
            .navigationBarTitle(Text("LIST"), displayMode: .inline)
            .navigationBarItems(//EDIT
                trailing:
                Group {
                    HStack (spacing: 15) {
                        self.editButton
                        self.delInfoButton
                        .contentShape(Rectangle())
                    }
                }
            )
        }
        
    }
    
   

    //MARK: EDIT MODE
    private func deleteItems() {
        
        DispatchQueue.global(qos: .userInteractive).async {
            Thread.current.name = #function
    
            selection.forEach{ idToRemove in
                if let index = allElements.firstIndex(where: { $0.id == idToRemove }) {
                    allElements.remove(at: index)
                }
            }
        }
    }
    
    private var editButton: some View {
        Button(action: {
            self.editMode.toggle()
            self.selection = Set<UUID>()
        }) {
            Text(self.editMode.title)
        }
    }

    private var delInfoButton: some View {
                
        if editMode == .inactive {
            return Button(action: {}) {
                Image(systemName: "square.and.arrow.up")
            }
        } else {
            return Button(action: deleteItems) {
                Image(systemName: "trash")
            }
        }
    }
   
}



struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


extension EditMode {
    var title: String {
        self == .active ? NSLocalizedString("done", comment: "") : NSLocalizedString("edit", comment: "")
    }

    mutating func toggle() {
        self = self == .active ? .inactive : .active
    }
}

2 个答案:

答案 0 :(得分:2)

您需要为所需内容类型的所有变体创建自定义处理的容器。

以下是关于以下内容支持示例的演示(通过List的示例)

LazyVStackSelectionable(allElements, selection: $selection) { element in
    Text(element.text)
}

使用Xcode 12 / iOS 14编写并测试了演示(它使用了某些SwiftUI 2.0功能,因此,如果需要SwiftUI 1.0支持,则需要更多调整)

demo

struct LazyVStackSelectionable<SelectionValue, Content> : View where SelectionValue : Hashable, Content : View {
    @Environment(\.editMode) var editMode
    
    private var selection: Binding<Set<SelectionValue>>?
    private var content: () -> Content
    private var editingView: AnyView?
    
    init(selection: Binding<Set<SelectionValue>>?, @ViewBuilder content: @escaping () -> Content)
    {
        self.selection = selection
        self.content = content
    }
    
    var body: some View {
        Group {
        if editingView != nil && self.editMode?.wrappedValue == .active {
            editingView!
        } else {
            self.content()
        }}
    }
}

extension LazyVStackSelectionable {
    init<Data, RowContent>(_ data: Data, selection: Binding<Set<SelectionValue>>?, @ViewBuilder rowContent: @escaping (Data.Element) -> RowContent) where Content == ForEach<Data, Data.Element.ID, HStack<RowContent>>, Data : RandomAccessCollection, RowContent : View, Data.Element : Identifiable, SelectionValue == Data.Element.ID
    {
        self.init(selection: selection, content: {
            ForEach(data) { el in
                HStack {
                    rowContent(el)
                }
            }
        })
        editingView = AnyView(
            ForEach(data) { el in
                HStack {
                    rowContent(el)
                    if let selection = selection {
                        Button(action: {
                            if selection.wrappedValue.contains(el.id) {
                                selection.wrappedValue.remove(el.id)
                            } else {
                                selection.wrappedValue.insert(el.id)
                            }
                        }) {
                             Image(systemName: selection.wrappedValue.contains(el.id) ? "checkmark.circle.fill" : "circle")
                        }
                    }
                }
            }
        )
    }
}

答案 1 :(得分:0)

我建议修改 LazyVStack 并将绑定传递给它,而不是创建自定义 ContentView

enter image description here

struct SampleRow: View {
    let element: MyElement
    let editMode: Binding<EditMode>
    let selection: Binding<Set<UUID>>?
    
    var body: some View {
        HStack {
            if editMode.wrappedValue == .active,
               let selection = selection {
                Button(action: {
                    if selection.wrappedValue.contains(element.id) {
                        selection.wrappedValue.remove(element.id)
                    } else {
                        selection.wrappedValue.insert(element.id)
                    }
                }) {
                     Image(systemName: selection.wrappedValue.contains(element.id) ? "checkmark.circle.fill" : "circle")
                }
            }
            Text("Sel Row \(element.number)")
        }
    }

    init(_ element: MyElement,
         editMode: Binding<EditMode>,
         selection: Binding<Set<UUID>>?) {
        print("Loading LazySampleRow row \(element.number)")
        self.editMode = editMode
        self.element = element
        self.selection = selection
    }
}

然后您可以将普通的 LazyVStack 包裹在 ScrollView 中以实现您的需要。

ScrollView {
    LazyVStack(alignment: .leading) {
        ForEach(allElements, id: \.self) {
            SampleRow($0,
                      editMode: $editMode,
                      selection: $selection)
        }
    }
}