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

SOLVED: EditButton on Empty List

Forums > SwiftUI

I have a List within a NavigationView. There is an EditButton on the navigation bar for removing items from the List.

If you delete all items in the list, the List is replaced by a message that there's "Nothing to see here folks...".

The issue I'm having is that when the List is empty:

  • the Edit button should toggle back to not editing (as there is nothing to edit)
  • the Edit button ideally should go away (again, there is nothing to edit)

Any ideas? All input appreciated

NOTE: This is a simplified example that demonstrates the behavior. The actual app is displaying from CoreData on CloutKit and there is a "Create" NavigatonLink available.

import SwiftUI

struct TestListView: View {

    @State var editing: Bool = false
    @State var names: [String] = [
        "Harry James",
        "James Connley",
        "Bob Newhart",
        "Sally Fields"
    ]

    var body: some View {
        NavigationView {

            if names.count == 0 {
                Text("Nothing to see here folks...")
            } else {
                List {
                    ForEach(names, id: \.self) { name in
                        Text(name)
                    }
                    .onDelete(perform: deleteItems)
                }
                .navigationBarItems(trailing: EditButton()
                    .onTapGesture {
                        self.editing.toggle()
                    })
                .navigationBarTitle("Names", displayMode: .inline)
                .navigationViewStyle(StackNavigationViewStyle())
            }
        }
    }

    private func deleteItems(offsets: IndexSet) {
        names.removeLast()
    }

}

struct TestListView_Previews: PreviewProvider {
    static var previews: some View {
        TestListView()
    }
}

2      

After playing around for a bit found a solution.

struct TestListView: View {
  @State var names: [String] = [
      "Harry James",
      "James Connley",
      "Bob Newhart",
      "Sally Fields"
  ]

  var body: some View {
      NavigationView {
          Group {
              if names.isEmpty {
                  Text("Nothing to see here folks...")
                      .navigationBarItems(trailing: EmptyView()) // Added a bar item to replace EditButton()
              } else {
                  List {
                      ForEach(names, id: \.self) {
                          Text($0)
                      }
                      .onDelete(perform: deleteItems)
                  }
                  .navigationBarItems(trailing: EditButton())
              }
          }
          .navigationBarTitle("Names", displayMode: .inline)
      }
  }

  private func deleteItems(at offsets: IndexSet) {
      names.remove(atOffsets: offsets)
  }
}

3      

Thanks @NigelGee - nice catch. That should work perfectly.

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.