是否可以在SwiftUI列表中以非编辑模式对行进行重新排序?
是给行添加一个右手汉堡菜单图标,他们可以用来重新排序行吗? (例如在编辑模式下可以实现)
答案 0 :(得分:1)
如果我正确理解了您的问题,请按以下步骤进行操作:
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()
}
}