SwiftUI List: swipe actions, search and pull to refresh
SwiftUI List explained: build a list with swipeActions, add a search field with searchable and pull to refresh with refreshable, with code to copy.
You build a list in SwiftUI with List: pass an array of Identifiable elements and describe what one row looks like. Swipe gestures, search and pull to refresh each take a single modifier: swipeActions, searchable and refreshable.
This post walks through all four building blocks using a small task list, with code you can copy.
SwiftUI List basics
SwiftUI needs a stable id for every element so it can tell rows apart. The simplest way is the Identifiable protocol.
struct TodoItem: Identifiable {
let id = UUID()
var title: String
}
struct TasksView: View {
@State private var tasks = [
TodoItem(title: "Groceries"),
TodoItem(title: "Workout")
]
var body: some View {
List(tasks) { task in
Text(task.title)
}
}
}List handles scrolling, separators and row reuse for you. Long lists stay smooth as long as the id is stable, so never generate it anew on each redraw.
Swipe gestures with swipeActions
For swipe actions, use ForEach inside the List and attach swipeActions to the row. The edge parameter decides which side the swipe starts from.
List {
ForEach(tasks) { task in
Text(task.title)
.swipeActions(edge: .trailing) {
Button("Delete", systemImage: "trash", role: .destructive) {
tasks.removeAll { $0.id == task.id }
}
}
.swipeActions(edge: .leading) {
Button("Done", systemImage: "checkmark") {
complete(task)
}
.tint(.green)
}
}
}- role: .destructive turns the button red and removes the row cleanly.
- tint gives other actions their own color.
- The first action fires on a full swipe. Turn that off with allowsFullSwipe: false.
If deleting is all you need, .onDelete on ForEach is enough. swipeActions pays off once there is more than one action.
A search field with searchable
searchable adds the system search field. You only provide the search text and filter the array yourself. The modifier needs navigation around the list, for example a NavigationStack.
@State private var query = ""
var filtered: [TodoItem] {
query.isEmpty ? tasks
: tasks.filter { $0.title.localizedCaseInsensitiveContains(query) }
}
var body: some View {
NavigationStack {
List(filtered) { task in
Text(task.title)
}
.navigationTitle("Tasks")
.searchable(text: $query, prompt: "Search tasks")
}
}localizedCaseInsensitiveContains ignores case and handles accented characters. When searching a database, filter in the query itself instead, as described in the post on SwiftData.
Pull to refresh with refreshable
refreshable adds the familiar gesture: pull the list down, a spinner appears, the data reloads. The closure is asynchronous, and the spinner disappears on its own once it finishes.
List(filtered) { task in
Text(task.title)
}
.refreshable {
await loadTasks()
}
.overlay {
if filtered.isEmpty {
ContentUnavailableView.search(text: query)
}
}The same example holds a second tip: ContentUnavailableView shows a ready-made message when the search finds nothing. An empty list without an explanation quickly looks like a bug.
Common List mistakes
- Unstable ids: Building the id from the index or creating it on every call leads to jumping rows and wrong animations.
- searchable without navigation: Without a NavigationStack or NavigationSplitView no search field appears.
- swipeActions on the List instead of the row: The modifier belongs on the row content.
- Too much work in a row: Heavy calculations and image loading don't belong in a row's body.
To animate list changes smoothly, see SwiftUI animations with withAnimation.