TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Programmatically toggle List onDelete action from custom cell

Forums > SwiftUI

Hello everyone! I'm teaching myself Swift while using SwiftUI (Xcode 12). I'm building an app that simulates the Contact app on iOS. I got the basics right except for one.

I can swipe a cell that shows the delete button but I want mine to behave just like Apple's where you click a button to toggle showing/hiding the delete button for the cell. See images:

And

How to toggle that delete action when I click the red image/icon? That button is located in the cell.

2      

I think this is what you are asking?

Add to view

.toolbar {
    EditButton()
}

See Paul's Adding swipe to delete and EditButton

2      

Hi Nigel.

Not really. I wont be showing an edit button on top right of NavigationBar. In fact, I do not have any for this view. Let's look at an example:


List {

   ForEach(0..<5, id: \.self) { num in {
       CellView(...)
   }
   .onDelete(...) // this works on swipe

}

[..]

// Inside CellView struct

HStack {

  Button("minus", action: {

    // What to call here to toggle the "delete slide out"?

  })

}

You see, my CellView has a button that when clicked should mimic a swipe action that brings out the delete button.

EDIT

This post gave me answer. What I needed was:


// body:

  List {
    //...
  }.environment(\.editMode, .constant(EditMode.active)) // <-- and this will always be active

2      

Have a

@State var isEditMode: EditMode = .inactive

then in modiflier

.environment(\.editMode, $isEditMode)

Then in your button

Button("minus", action: {
  isEditMode = .active
})

Try that have not tested myself but looking at the Stack Overflow seem what to do

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your 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.