Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crash if swiping to delete a cell of UITableView quickly and continuously in iOS11

The problem is like the title. Here is my code. And I find the operation, swipe from the right to left to delete directly, is new feature in iOS11

let model = self.customRules.remove(at: indexPath.row)  //delete datasource
self.dao.delete(model: model) //delete data in database
tableView.beginUpdates()  //still crash no matter the line exists or not
tableView.deleteRows(at: [indexPath], with: .left) //delete cell view
tableView.endUpdates()

This is the crash log.

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView internal inconsistency: the _swipedIndexPath cannot be nil if the swipe to delete row is being deleted in _updateRowsAtIndexPaths:withUpdateAction:rowAnimation:usingPresentationValues:'

Last Exception Backtrace:
0   CoreFoundation                      0x000000018710fd50 <redacted> + 148
1   libobjc.A.dylib                     0x0000000186624528 objc_exception_throw + 56
2   CoreFoundation                      0x000000018710fc0c <redacted> + 0
3   Foundation                          0x0000000187a9bb44 <redacted> + 112
4   UIKit                               0x00000001907c52b8 <redacted> + 648
5   UIKit                               0x00000001906819e4 <redacted> + 140
......
like image 274
Victor Choy Avatar asked Aug 29 '17 02:08

Victor Choy


2 Answers

Your code seems to be OK, but there has been a bug in Xcode 9 with the deletion of cells. If you try your code in Xcode 8 it will probably work. Checkout this answer for more information.

like image 74
Rashwan L Avatar answered Oct 10 '22 12:10

Rashwan L


Try to use my example.

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    var data = ["Byba", "Boba", "Gent", "Xpa", "zc", "123", "swipe", "gen", "sw", "xcas", "kjs", "908"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

//  MARK: - UITableViewDelegate
extension ViewController: UITableViewDelegate {

    func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        return "delete"
    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            tableView.beginUpdates()
            data.remove(at: indexPath.row)
            tableView.deleteRows(at: [indexPath], with: .left)
            tableView.endUpdates()
        }
    }
}

//  MARK: - UITableViewDataSource
extension ViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        let item = data[indexPath.row]

        cell.textLabel?.text = item

        return cell
    }
}
like image 27
LLIAJLbHOu Avatar answered Oct 10 '22 12:10

LLIAJLbHOu