All Item Row Positions After List onMove(perform:)

Using List to list an array of an object isn't a problem. For example, I have simple lines of code below to list an array of some guy.

struct ContentView: View {
    @State private var selectedFieldItem: FieldItem?
    
    private var fieldListView: some View {
        List(selection: $selectedFieldItem) {
            ForEach(fieldItems.indices, id: \.self) { index in
                Button {
                    ...
                } label: {
                    let fieldItem = fieldItems[index]
                    HStack(spacing: 10) {
                        Text("\(fieldItem.name)")
                    }
                    .frame(maxWidth: .infinity, alignment: .leading)
                }
                .buttonStyle(.borderless)
            }
            .onMove(perform: fieldlocate)
        }
        .listStyle(.plain)
    }

    private func fieldlocate(from source: IndexSet, to destination: Int) {
        fieldItems.move(fromOffsets: source, toOffset: destination)
    }
}

As you see in the picture below, I can move a row up and down.

A problem that I now have with List is that I cannot know how those rows are rearranged after one of them is moved up or down. In Cocoa, you can tell the positions of all rows after one moves with

tableView(_:acceptDrop:row:dropOperation:)

I think I have done the same in UIKit. Can we tell the current row numbers of List items in SwiftUI? Like

Item 0 moves from Row 0 to Row 2

item 1 moves from Row 1 to Row 0

item 2 moves from Row 2 to Row 1

after one drags Item 0 from the top to the bottom?

Muchos thankos.

Answered by Tomato in 799553022

Actually, all I have to do is enumerate an array inside the onMove(perform:) guy.

Accepted Answer

Actually, all I have to do is enumerate an array inside the onMove(perform:) guy.

All Item Row Positions After List onMove(perform:)
 
 
Q