Home > Back-end >  How to show a done button depending on the edition status of a tableview, from a UITableView extensi
How to show a done button depending on the edition status of a tableview, from a UITableView extensi

Time:06-04

I wrote a UITableView extension that would allow the reordering of cells after holding the tableview.

So the tableView goes edit mode as expected but what I would like to do is to add a Done button in the navigation bar of a viewController, when the tableView is being edited and that would end the edit mode when tapped.

How can I show/hide this button in viewController according to if the tableView being edited or not in UITableView extension?

that is my extension:

import UIKit

extension UITableView {
    func addLongPressToTableView() {
        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(onLongPressGesture(sender:)))
        longPress.minimumPressDuration = 0.8 // optional
        self.addGestureRecognizer(longPress)
    }
    
    @objc func onLongPressGesture(sender: UILongPressGestureRecognizer) {
        if (sender.state == .began) {
            self.isEditing = true
            self.setEditing(true, animated: false)
            UIImpactFeedbackGenerator(style: .light).impactOccurred()
        }
    }
}

// ViewController, hide/show editButtonItem (done button?)
// navigationItem.rightBarButtonItems = [addButton, editButtonItem]

CodePudding user response:

The UITableViewDelegate provides support for coordinating editing. Use the following two delegate methods to respond to changes in the editing state.

func tableView(UITableView, willBeginEditingRowAt: IndexPath)

func tableView(UITableView, didEndEditingRowAt: IndexPath?)

  • Related