0Pricing
SwiftUI Academy · Lekcja

Usuwanie wierszy gestem przesunięcia

Proszę usuwać elementy za pomocą onDelete.

Usuwanie wierszy gestem przesunięcia to bezpłatna lekcja SwiftUI Academy na CoddyKit. To lekcja 1 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.

Lists That Can Lose Rows

A static list just shows data. Real apps let people remove items, and SwiftUI makes deletion feel native with a simple swipe. ✨

Meet onDelete

The onDelete modifier attaches to a ForEach inside a List. It enables the swipe-left gesture that reveals a red Delete button.

Your Data Lives in State

To delete a row you must own the data. Keep your array in @State so changing it instantly refreshes the list on screen.

struct Item: Identifiable {
    let id = UUID()
    var name: String
}

Declaring the Array

Hold your items in a mutable @State array. When you remove an element, SwiftUI animates the row away for free.

@State private var items = [
    Item(name: "Milk"),
    Item(name: "Eggs")
]

The IndexSet Argument

onDelete hands you an IndexSet of the rows the user swiped. It is a set because a person could remove several at once.

Removing by Offsets

Pass that IndexSet straight to the arrays remove(atOffsets:) method. One line removes exactly the swiped items safely.

func delete(at offsets: IndexSet) {
    items.remove(atOffsets: offsets)
}

Wiring It Together

Put your rows in a ForEach inside a List, then attach onDelete to call your delete function. That is the whole pattern.

List {
    ForEach(items) { item in
        Text(item.name)
    }
    .onDelete(perform: delete)
}

Inline Closures Work Too

You can skip the named function and write the deletion right inside onDelete as a closure when the logic is tiny.

.onDelete { offsets in
    items.remove(atOffsets: offsets)
}

Free Swipe Animation

Because items is @State, SwiftUI compares before and after and slides the deleted row out smoothly. You never write the animation yourself.

Attach to ForEach, Not List

A common slip is putting onDelete on the List. It belongs on the ForEach, since that is what maps your array to rows.

Custom Swipe Actions

For more than delete, reach for swipeActions, which lets you add tinted buttons like Archive or Flag on either edge.

.swipeActions {
    Button("Flag") { }
        .tint(.orange)
}

Quick Check

Where does the onDelete modifier belong?

Recap: Swipe-to-Delete

You learned to keep items in @State, attach onDelete to a ForEach, and remove swiped rows with remove(atOffsets:). 🎉

Często zadawane pytania

Czy lekcja „Usuwanie wierszy gestem przesunięcia” jest bezpłatna?

Tak — pełny tekst „Usuwanie wierszy gestem przesunięcia” 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 „Usuwanie wierszy gestem przesunięcia”?

Proszę usuwać elementy za pomocą onDelete. Ć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 1 z 4.

Ile czasu zajmuje lekcja „Usuwanie wierszy gestem przesunięcia”?

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