7

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 Answer 1

8

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)
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

hi - I think that's working because it's in edit mode correct, where I'm after a way to allow one always to re-order, so a way to have "onMove" work when not in edit mode...
EditMode is just a state value, you need to set it for local list to have row-reordering style. Those things are dependent, no first - no second. Alternate is to code completely custom List-like view.
ok thanks - so basically not possible - good to know. I quite like GoApps (apps.apple.com/us/app/gotasks/id389113399) which is setup like I was referring to and wondering how to get this. A DIY list to do this would then be quite tricky right, having to hand drag/drop events etc?
As far as I can see GoApps uses standard reordering activated by Edit button. If this is what you want, it is even simpler - just add anywhere EditButton().
in the list view of GoApps they seemed to have managed not to have the delete button showing (which is what I was hoping to avoid) and replace this with a check box - not sure if this is possible in SwiftUI?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.