4

Is it possible to "re-order"/"move" items in a ScrollView as one can in a List? I have playground below which compiles/run fine, however the onDelete / onMove does does visualise any delete button or hamburger lines for dragging/moving?

Is there a way to get my code working here so at least the "onMove" concept works?

Note - I note that both List and ScrollView (I think) support protocol DynamicViewContent, so was assuming it should work(?)

Image after running code:

enter image description here

Code (Playgrounds)

import SwiftUI
import PlaygroundSupport

let modelData: [String] = ["Eggs", "Milk", "Bread"]

struct ListTestNoDelete: View {
    private func move(from uiStartIndexSet: IndexSet, to uiDestIndex: Int) {
        print("On Move")
    }
    private func delete(at offsets : IndexSet) {
        print("On Delete")
    }
    var body: some View {
        NavigationView {
            VStack {
                ScrollView {
                    ForEach(modelData, id: \.self) { str in
                        HStack {
                            Image(systemName: "square")
                            Text(str)
                            Spacer()
                        }.padding(.horizontal, 3)
                    }
                    .onMove(perform: self.move)
                    .onDelete(perform: self.delete)
                }
                .environment(\.editMode, .constant(.active))
                .navigationBarTitle( Text("Test") )
            }
        }
    }
}


let listTest = ListTestNoDelete()
PlaygroundPage.current.liveView = UIHostingController(rootView: listTest)
Greg
  • 34,042
  • 79
  • 253
  • 454
  • please read and consider this https://stackoverflow.com/help/how-to-ask and please show us your code you tried so far. – Chris Apr 18 '20 at 12:57
  • good point - done – Greg Apr 18 '20 at 23:22
  • i think your question is answered here....https://stackoverflow.com/questions/57568895/how-to-remove-list-selection-indicator-and-separator-in-swiftui – Chris Apr 19 '20 at 05:50
  • I don't see how this answers it, however it does say that it doesn't support it. It mentions ScrollView but then confirms that onMove doesn't work for ScrollView – Greg Apr 19 '20 at 07:24
  • 1
    just read it: quote: "EDIT: you can use onDelete, onMove and onInsert only within a List. If you want to let users delete a row in a ScrollView you must implement something yourself. Take a look at the code below for a simple (pretty ugly) example:" – Chris Apr 19 '20 at 12:45

0 Answers0