NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

List view stuck in edit mode

Forums > SwiftUI

I have an editable list that allows the user to add and delete items. There is an Add button to add items to the list and the EditButton view along with an onDelete method allows items to be deleted from the list either by swiping right to left, or by tapping the Edit button and then tapping the delete icon.

All is working well when the user swipes from right to left to delete items from the list. When deleting items the Edit button changes to Done, but reverts to Edit when the item has been deleted.

However, if the Edit button is tapped to enter edit mode (Edit button changes to Done) to delete items using the delete icon and confirming the delete, items are deleted okay but when the last item is deleted and the list is empty, the list stays in edit mode.

Is there any way to automatically leave Edit Mode when the list is empty?

What I ultimately want is to display a "Tap + to add a list" text view and hide the EditButton when the list is empty, which I can do, but when the user adds item(s) to an empty list and the EditButton is displayed again, it's still in edit mode i.e. displays Done, not Edit. This looks strange to me and could confuse the user.

Here is some example code I'm using for the editable list:

struct ContentView: View {
    @State private var users = ["Mary", "Mungo", "Midge"]

    var body: some View {
        NavigationView {
            List {
                ForEach(users, id: \.self) { user in
                    Text(user)
                }
                .onDelete(perform: delete)
            }
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) {
                    Button(action: add) {
                        Label("Add", systemImage: "plus")
                    }
                }
                ToolbarItem(placement: .navigationBarTrailing) {
                    EditButton()
                }
            }
            .navigationTitle("Users")
        }
    }

    func add() {
        users.append("New User")
    }

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

Here's a link to a short video file showing the empty list staying in edit mode until the Done button is tapped editable-list

Thanks.

Gavin

   

Hacking with Swift is sponsored by Instabug

SPONSORED Catch bugs as soon as they happen and know exactly why a crash occurred. Instabug's SDK grabs all the logs they need to fix bugs, crashes and performance issues in minutes instead of days. Get screenshots, device details, network logs, repro steps, and tons of other critical insights needed to resolve issues and prioritize product backlogs straight from your dashboard. It only takes a minute to integrate!

Get started now

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.