如何在不在编辑模式下的 SwiftUI 列表中重新排序行? [英] How to allow reordering of rows in a SwiftUI List whist NOT in edit mode?

查看:29
本文介绍了如何在不在编辑模式下的 SwiftUI 列表中重新排序行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否允许在不在编辑模式下的 SwiftUI 列表中重新排序行?

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)

推荐答案

如果我正确理解了您的问题,那么可以这样做:

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

这篇关于如何在不在编辑模式下的 SwiftUI 列表中重新排序行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆