【Xcode/SwiftUI】リストアイテムの並び替えを行う方法

コーディング

President.swift (構造体)

import SwiftUI

struct President: Identifiable {
    var id: Int
    let name: String

    init(id: Int, name: String) {
        self.id = id
        self.name = name
    }
}

HomeView.swift (メインのView)

import SwiftUI

struct HomeView: View {
    @State private var president: [President] = [
        President(id: 1, name: "Joe Biden"),
        President(id: 2, name: "Donald Trump"),
        President(id: 3, name: "Barack Obama"),
        President(id: 4, name: "George W. Bush"),
        President(id: 5, name: "Bill Clinton"),
        President(id: 6, name: "George H. W. Bush"),
        President(id: 7, name: "Ronald Reagan"),
        President(id: 8, name: "Jimmy Carter"),
        President(id: 9, name: "Gerald Ford"),
        President(id: 10, name: "Richard Nixon")
    ]

    var body: some View {
        NavigationView {
            List {
                ForEach(president.indices, id: \.self) { index in
                    HStack {
                        ZStack {
                            Circle()
                                .foregroundColor(.orange)
                                .frame(width: 24)
                            Text("\(president[index].id)")
                                .foregroundColor(.white)
                        }
                        Text(president[index].name)
                    }
                }
                .onMove { (indexSet, index) in
                    self.president.move(fromOffsets: indexSet, toOffset: index)
                    // セルが動かされた時に番号の順番もアップデートする
                    for (index, _) in self.president.enumerated() {
                        self.president[index].id = index + 1
                    }
                }
            }
            .navigationBarTitle(Text("US Presidents"))
            .navigationBarItems(trailing: EditButton())
        }
    }
}

struct HomeView_Previews: PreviewProvider {
    static var previews: some View {
        HomeView()
    }
}