0Pricing
SwiftUI Academy · Lekcja

Zmiana kolejności za pomocą onMove

Proszę przeciągać wiersze, aby zmieniać ich kolejność.

Zmiana kolejności za pomocą onMove to bezpłatna lekcja SwiftUI Academy na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej SwiftUI Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs SwiftUI Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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

Często zadawane pytania

Czy lekcja „Zmiana kolejności za pomocą onMove” jest bezpłatna?

Tak — pełny tekst „Zmiana kolejności za pomocą onMove” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu SwiftUI Academy, przejdź na CoddyKit PRO. Kurs SwiftUI Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „Zmiana kolejności za pomocą onMove”?

Proszę przeciągać wiersze, aby zmieniać ich kolejność. Ćwiczysz SwiftUI Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć SwiftUI Academy?

Nie wymagamy żadnego doświadczenia. SwiftUI Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.

Ile czasu zajmuje lekcja „Zmiana kolejności za pomocą onMove”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji SwiftUI Academy?

Tak. Każda lekcja SwiftUI Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Usuwanie wierszy gestem przesunięcia
  2. Zmiana kolejności za pomocą onMove
  3. Dodawanie nowych elementów
  4. Odświeżanie gestem przeciągnięcia
← Powrót do SwiftUI Academy