Dodawanie nowych elementów
Proszę dodawać wiersze i utrzymywać synchronizację listy.
Dodawanie nowych elementów to bezpłatna lekcja SwiftUI Academy na CoddyKit. To lekcja 3 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.
Growing the List
A useful list does not stay frozen. Letting users add items is the other half of editing, and it keeps your @State array in charge. ✨
Append to Your Array
Adding a row is just adding data. Call append on your @State array and SwiftUI inserts a fresh row automatically.
items.append(Item(name: "Bread"))A Toolbar Add Button
Most apps add items from a plus button. Put a Button in the toolbar that calls your add logic when tapped.
.toolbar {
Button("Add") { addItem() }
}Use a System Plus Icon
Swap the text for an SF Symbol so it reads as Add. The plus symbol is the universal cue users already understand.
Button {
addItem()
} label: {
Image(systemName: "plus")
}Writing addItem
Your addItem function creates a new model value and appends it. Keep it small and let the array drive the UI.
func addItem() {
items.append(Item(name: "New"))
}Add From a TextField
Often the new item comes from typing. Bind a TextField to a draft string, then append that text when the user confirms.
@State private var draft = ""
TextField("Item", text: $draft)Append Then Clear
After appending, reset the draft so the field is empty for the next entry. Clearing draft keeps the form feeling fresh.
items.append(Item(name: draft))
draft = ""Guard Against Blanks
Do not add empty rows. A quick guard on the trimmed draft stops accidental blank items from cluttering your list.
guard !draft.trimmingCharacters(
in: .whitespaces).isEmpty
else { return }New Rows Animate In
Because the array changed, SwiftUI slides the new row into place. Wrap the append in withAnimation for an even snappier feel.
withAnimation {
items.append(Item(name: draft))
}Insert at the Top
Want newest first? Use insert(at:) with index 0 instead of append, and the row appears at the top of the list.
items.insert(Item(name: draft),
at: 0)One Source of Truth
Add, delete, and move all mutate the same @State array. That single source of truth keeps your whole list consistent.
Quick Check
How do you add a new row to a SwiftUI list?
Recap: Adding Items
You can grow a list by appending to its @State array, often from a toolbar button or a TextField, with guards and animation. 🎉
Ucz się Swift dzięki korepetycjom AI — za darmo
Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.
- Kursy
- 30
- Lekcje
- 120
Często zadawane pytania
Czy lekcja „Dodawanie nowych elementów” jest bezpłatna?
Tak — pełny tekst „Dodawanie nowych elementów” 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 „Dodawanie nowych elementów”?
Proszę dodawać wiersze i utrzymywać synchronizację listy. Ć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 3 z 4.
Ile czasu zajmuje lekcja „Dodawanie nowych elementów”?
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
- Usuwanie wierszy gestem przesunięcia
- Zmiana kolejności za pomocą onMove
- Dodawanie nowych elementów
- Odświeżanie gestem przeciągnięcia