Greg
Greg

Reputation: 34798

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

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)

Upvotes: 7

Views: 3536

Answers (1)

Asperi
Asperi

Reputation: 257493

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

Upvotes: 7

Related Questions