Skip to content

Instantly share code, notes, and snippets.

@mariosaputra
Created February 27, 2024 08:42
Show Gist options
  • Save mariosaputra/6acb39cca174a7d73e2eb0aa282ab182 to your computer and use it in GitHub Desktop.
Save mariosaputra/6acb39cca174a7d73e2eb0aa282ab182 to your computer and use it in GitHub Desktop.
Moving List
struct Goal: Identifiable, Equatable {
var id = UUID()
var title: String
var iconName: String
}
import SwiftUI
struct ContentView: View {
@State private var goals = [
Goal(title: "Learn SwiftUI", iconName: "book.fill"),
Goal(title: "Build an indie app", iconName: "hammer.fill"),
Goal(title: "Launch on the App Store", iconName: "apple.logo"),
Goal(title: "Market the app", iconName: "megaphone.fill")
]
// State to control the edit mode
@State private var isEditing = false
var body: some View {
NavigationView {
List {
ForEach(goals) { goal in
HStack {
Image(systemName: goal.iconName)
.foregroundColor(.blue)
.frame(width: 30, alignment: .center)
Text(goal.title)
}
}
.onMove(perform: move)
.onDelete(perform: delete)
}
.navigationTitle("My Goals")
.toolbar {
EditButton()
}
}
}
func move(from source: IndexSet, to destination: Int) {
goals.move(fromOffsets: source, toOffset: destination)
}
func delete(at offsets: IndexSet) {
goals.remove(atOffsets: offsets)
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment