Created
October 23, 2024 16:15
-
-
Save takoikatakotako/1950b10a59d999cf7fc443dd67f54350 to your computer and use it in GitHub Desktop.
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() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment