SwiftUI Academy · Lektion

Mit onMove neu anordnen

Ziehen Sie Zeilen, um sie neu anzuordnen.

Lektion 2 von 413 Schritte

Mit onMove neu anordnen ist eine kostenlose SwiftUI Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des SwiftUI Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

Let Users Reorder Rows

Sometimes order matters, like a to-do priority list. SwiftUI lets people drag rows into a new order with almost no extra code. ✨

Meet onMove

The onMove modifier sits on your ForEach and enables drag handles so rows can be picked up and dropped elsewhere.

The Move Signature

onMove gives you two things: an IndexSet of the rows being moved and an Int destination index where they should land.

func move(from source: IndexSet,
          to destination: Int) {
}

One Array Method Does It

Swift arrays have a built-in move(fromOffsets:toOffset:) that performs the reorder for you in a single, safe call.

items.move(fromOffsets: source,
           toOffset: destination)

Wiring onMove Up

Attach onMove(perform:) to the ForEach and point it at your move function. The list now supports drag-to-reorder.

ForEach(items) { item in
    Text(item.name)
}
.onMove(perform: move)

Edit Mode Reveals Handles

Drag handles often appear only in edit mode. An EditButton in the toolbar flips the list into editing so users can reorder.

.toolbar {
    EditButton()
}

Reorder Without Edit Mode

Inside a plain List, rows are frequently draggable straight away. Edit mode mainly matters when you also expose delete and move controls.

State Keeps Order Persistent

Because move() mutates your @State array, the new order survives view refreshes and reflects everywhere that array is read.

Combine Move and Delete

You can attach both onMove and onDelete to the same ForEach, giving users full control to reorder and remove rows.

ForEach(items) { Text($0.name) }
    .onDelete(perform: delete)
    .onMove(perform: move)

Inline Move Closure

Like delete, you can write the reorder inline. Just call move(fromOffsets:toOffset:) directly inside the onMove closure.

.onMove { source, dest in
    items.move(fromOffsets: source,
               toOffset: dest)
}

Smooth Drag Animations

SwiftUI animates the lift, slide, and settle of dragged rows automatically. You get polished motion with zero animation code.

Quick Check

What does onMove provide to your handler?

Recap: Reordering

You added drag-to-reorder by attaching onMove to a ForEach and calling move(fromOffsets:toOffset:) on your state array. 🎉

Kostenlos starten

Lerne Swift mit einem KI-Tutor — kostenlos

Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.

Kurse
30
Lektionen
120

Häufig gestellte Fragen

Ist die Lektion „Mit onMove neu anordnen“ kostenlos?

Ja — der vollständige Text von „Mit onMove neu anordnen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des SwiftUI Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Mit onMove neu anordnen“?

Ziehen Sie Zeilen, um sie neu anzuordnen. Du übst SwiftUI Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um SwiftUI Academy zu starten?

Keine Vorkenntnisse erforderlich. SwiftUI Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „Mit onMove neu anordnen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser SwiftUI Academy-Lektion Code schreiben und ausführen?

Ja. Jede SwiftUI Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Zeilen per Wischgeste löschen
  2. Mit onMove neu anordnen
  3. Neue Elemente hinzufügen
  4. Zum Aktualisieren ziehen
← Zurück zu SwiftUI Academy