Home > OS >  Issue Editing Mode by swiped cell in tableView
Issue Editing Mode by swiped cell in tableView

Time:09-17

I have the following problem. If I swipe the cell only until the delete button is visible and then click on edit, the table is not in editing mode but the button shows it.

First Swipe until delete is visible Edit Button pressed

I have already seen with the methods shouldBeginEditingAtRow and didEndEditingAtRow that if I first swipe a row until the delete button and then just swipe the next row, that the status of the tableView.isEditing is true. That means if I swipe a row until delete button is visible and then click edit, setEditing will set the status of the tableView to false.

I have already tried to set the status to false in the didEndEditingAtRow method.But unfortunately this did not work. Do you have a tip or a solution for me?

My code:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    var data = [1,2,3,4,5,6,7,8,9]
    
    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(!isEditing, animated: true)
        tableView.setEditing(!tableView.isEditing, animated: true)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = String(data[indexPath.row])
        return cell
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            data.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .automatic)
        }
    }

    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        tableView.dataSource = self
        tableView.delegate = self
        
        
        navigationItem.rightBarButtonItem = editButtonItem
    }


}

CodePudding user response:

First, whenever you are overriding a UIKit Method try to pass the parameters from the method you are overriding So update setEditing method with these lines:

   override func setEditing(_ editing: Bool, animated: Bool) {
         super.setEditing(editing, animated: true)
             //handle Animation
             if editing  {
               self.tableView.reloadSections(IndexSet(integersIn: 0...0), with: .none)
             }
         tableView.setEditing(editing, animated: true)
 }
  • Related