UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: 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

3      

Have you tried this:

struct ContentView: View {
    @Environment(\.editMode) var editMode
    @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")
        }
        .onChange(of: users) { newValue in
            if editMode?.wrappedValue == .active && users.count == 0 {
                editMode?.wrappedValue = .inactive
            }
        }
    }

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

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

3      

Hmmm, I tried your suggestion but found that editMode?.wrappedValue was always .inactive.

However, your answer prompted me to do some more googling around editMode and I found this article which gave me the solution, so thanks for your answer.

3      

For those interested, here's the solution I came up with:

struct ContentView: View {
    // https://www.vadimbulavin.com/add-edit-move-and-drag-and-drop-in-swiftui-list/
    @State private var editMode = EditMode.inactive
    @State private var items = (0..<3).map { "Item #\($0)" }

    var body: some View {
        NavigationView {
            List {
                ForEach(items, id: \.self) { user in
                    Text(user)
                }
                .onDelete(perform: delete)
                .onMove(perform: onMove)
            }
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) { addButton }
                ToolbarItem(placement: .navigationBarTrailing) { editButton }
            }
            .navigationTitle("Users")
            .environment(\.editMode, $editMode)
            .onChange(of: items) { newValue in
                if editMode == .active && items.count == 0 {
                    editMode = .inactive
                }
            }
        }
    }

    private var editButton: some View {
        return Group {
            switch editMode {
            case .inactive:
                if items.count == 0 {
                    EmptyView()
                } else {
                    EditButton()
                }
            case .active:
                EditButton()
            default:
                EmptyView()
            }
        }
    }

    private var addButton: some View {
        return Group {
            switch editMode {
            case .inactive:
                Button(action: add) { Label("Add", systemImage: "plus") }
            default:
                EmptyView()
            }
        }
    }

    private func add() {
        items.append("New Item")
    }

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

    private func onMove(source: IndexSet, destination: Int) {
        items.move(fromOffsets: source, toOffset: destination)
    }
}

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.