Skip to content

Instantly share code, notes, and snippets.

@takoikatakotako
Created October 23, 2024 16:15
Show Gist options
  • Select an option

  • Save takoikatakotako/1950b10a59d999cf7fc443dd67f54350 to your computer and use it in GitHub Desktop.

Select an option

Save takoikatakotako/1950b10a59d999cf7fc443dd67f54350 to your computer and use it in GitHub Desktop.
SwiftUIでListをEditModeにして並び替える
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