SwiftUIでListをEditModeにして並び替える
SwiftUIでListをEditModeにして並び替える方法です。

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct ContentView: View { | |
@State var pokemons: [Pokemon] = [ | |
Pokemon(id: 143, name: "Snorlax"), | |
Pokemon(id: 52, name: "Meowth"), | |
Pokemon(id: 25, name: "Pikachu") | |
] | |
var body: some View { | |
NavigationStack { | |
List { | |
ForEach(pokemons) { pokemon in | |
Text(pokemon.name) | |
}.onMove { (indexSet, index) in | |
pokemons.move(fromOffsets: indexSet, toOffset: index) | |
} | |
} | |
.navigationBarTitle(Text("Pokemon List")) | |
.navigationBarItems(trailing: EditButton()) | |
} | |
} | |
} | |
#Preview { | |
ContentView() | |
} |