0Pricing
SwiftUI Academy · Lesson

Reordering with onMove

Drag rows to reorder them.

Reordering with onMove is a free SwiftUI Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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. 🎉

Frequently asked questions

Is the “Reordering with onMove” lesson free?

Yes — the full text of “Reordering with onMove” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.

What will I learn in “Reordering with onMove”?

Drag rows to reorder them. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start SwiftUI Academy?

No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Reordering with onMove” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this SwiftUI Academy lesson?

Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Swipe-to-Delete Rows
  2. Reordering with onMove
  3. Adding New Items
  4. Pull-to-Refresh
← Back to SwiftUI Academy