List and ForEach
A Simple List
swift
List {
Text("Apple")
Text("Banana")
Text("Cherry")
}
List from Data
swift
struct Fruit: Identifiable {
let id = UUID()
let name: String
let emoji: String
}
struct FruitList: View {
let fruits = [
Fruit(name: "Apple", emoji: "?"),
Fruit(name: "Banana", emoji: "?"),
Fruit(name: "Cherry", emoji: "?")
]
var body: some View {
List(fruits) { fruit in
HStack {
Text(fruit.emoji)
Text(fruit.name)
}
}
}
}
Why Identifiable? SwiftUI needs a stable unique
id for each row so it knows which row changed, moved or was deleted. If your model has no id, use id: \.name or similar: List(fruits, id: \.name).
Sections and Styles
swift
List {
Section("Fruits") {
ForEach(fruits) { Text($0.name) }
}
Section("Settings") {
Text("Profile")
Text("Help")
}
}
.listStyle(.insetGrouped) // .plain, .grouped, .sidebar
Add, Delete and Move
swift
struct EditableList: View {
@State private var items = ["One", "Two", "Three"]
var body: some View {
NavigationStack {
List {
ForEach(items, id: \.self) { item in
Text(item)
}
.onDelete { offsets in items.remove(atOffsets: offsets) }
.onMove { from, to in items.move(fromOffsets: from, toOffset: to) }
}
.navigationTitle("Items")
.toolbar {
EditButton()
Button("Add", systemImage: "plus") { items.append("New \(items.count + 1)") }
}
}
}
}
Swipe Actions
swift
ForEach(tasks) { task in
Text(task.title)
.swipeActions(edge: .trailing) {
Button("Delete", role: .destructive) { delete(task) }
}
.swipeActions(edge: .leading) {
Button("Done", systemImage: "checkmark") { complete(task) }
.tint(.green)
}
}
Search and Pull to Refresh
swift
struct SearchableList: View {
@State private var query = ""
let names = ["Asha", "Ravi", "Meena", "Kabir"]
var filtered: [String] {
query.isEmpty ? names : names.filter { $0.localizedCaseInsensitiveContains(query) }
}
var body: some View {
NavigationStack {
List(filtered, id: \.self) { Text($0) }
.navigationTitle("People")
.searchable(text: $query, prompt: "Search names")
.refreshable {
try? await Task.sleep(for: .seconds(1)) // reload data here
}
}
}
}
Performance:
List loads rows lazily, so it handles thousands of rows. Keep row views small and avoid heavy work inside them.
Conclusion
List and ForEach are your main tools for showing collections. Give every item a stable id, then add sections, swipe actions and search.
Quick Summary: List(data) shows rows. Use Identifiable, Section, onDelete, swipeActions, searchable and refreshable.

