NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

How to let users delete rows from a list

Paul Hudson    @twostraws   

Updated for Xcode 14.2

SwiftUI provides two ways to let us delete rows from a list: a simple way supported on iOS 16.0 or later, and a more advanced way that works on older iOS versions too. Regardless of which approach you choose, you can also selectively disable deletion for rows using the deleteDisabled() modifier.

The simple approach to deletion works great if you just want users to swipe to delete items from an array, without adding any additional logic. To use it, use a data binding with your list and pass in the editActions parameter, like this:

struct ContentView: View {
    @State private var users = ["Glenn", "Malcolm", "Nicola", "Terri"]

    var body: some View {
        NavigationStack {
            List($users, id: \.self, editActions: .delete) { $user in
                Text(user)
            }
        }
    }
}

Download this as an Xcode project

That immediately lets users swipe to delete rows, and the users array will be updated as they do so. If you want to let them move the items as well, use .all rather than just .delete.

If you want to disable deletion for one row, use deleteDisabled() with whatever criteria you have. For example, we could say that the user can delete freely from the list as long as there’s always at least 1 row remaining:

struct ContentView: View {
    @State private var users = ["Glenn", "Malcolm", "Nicola", "Terri"]

    var body: some View {
        NavigationStack {
            List($users, id: \.self, editActions: .delete) { $user in
                Text(user)
                    .deleteDisabled(users.count < 2)
            }
        }
    }
}

Download this as an Xcode project

For the more complex approach to deletion, we can attach an onDelete(perform:) modifier to a ForEach inside a list, and have it call a method of our choosing when a delete operation happens. This handler needs to have a specific signature that accepts multiples indexes to delete, like this:

func delete(at offsets: IndexSet) {
    // delete the objects here
}

Inside there you will usually want to call Swift’s remove(atOffsets:) method to delete the requested rows from your sequence. Because SwiftUI is watching your state, any changes you make will automatically be reflected in your UI.

For example, this code creates a ContentView struct with a list of three items, then attaches an onDelete(perform:) modifier that removes any item from the list:

struct ContentView: View {
    @State private var users = ["Paul", "Taylor", "Adele"]

    var body: some View {
        NavigationStack {
            List {
                ForEach(users, id: \.self) { user in
                    Text(user)
                }
                .onDelete(perform: delete)
            }
            .navigationTitle("Users")
        }
    }

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

Download this as an Xcode project

Three rows in an iOS list, with the middle one showing a delete button after a swipe.

If you run that code you’ll find you can swipe to delete any row in the list.

Tip: In case you were wondering, onDelete() exists as a modifier for ForEach but not for List directly. This is because lists can include static rows, which of course cannot be deleted.

Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.2/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.