How to allow reordering of rows in a SwiftUI List whist NOT in edit mode?

倾然丶 夕夏残阳落幕 提交于 2020-01-24 00:12:10

问题


Is there away to allow reordering of rows in a SwiftUI List whist NOT in edit mode?

That is to give rows a right hand hamburger menu icon which they can use to re-order a row? (like which is possible in edit mode)


回答1:


If I understood your question correctly, here is how it is possible to be done:

import SwiftUI

struct TestEditModeCustomRelocate: View {
    @State private var objects = ["1", "2", "3"]
    @State var editMode: EditMode = .active

    var body: some View {
        List {
            ForEach(objects, id: \.self) { object in
                Text("Row \(object)")
            }
            .onMove(perform: relocate)
        }
        .environment(\.editMode, $editMode)
    }

    func relocate(from source: IndexSet, to destination: Int) {
        objects.move(fromOffsets: source, toOffset: destination)
    }
}

struct TestEditModeCustomRelocate_Previews: PreviewProvider {
    static var previews: some View {
        TestEditModeCustomRelocate()
    }
}


来源:https://stackoverflow.com/questions/58765661/how-to-allow-reordering-of-rows-in-a-swiftui-list-whist-not-in-edit-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!