如果在iOS11中快速连续地删除UITableView的某个单元格时发生崩溃

问题描述:

问题就像标题一样。这是我的代码。我发现操作,从右向左轻扫直接删除,是iOS11的新功能如果在iOS11中快速连续地删除UITableView的某个单元格时发生崩溃

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() 

这是崩溃日志。

*** 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 
...... 
+0

您是否有崩溃记录?另外,你怎么知道这是iOS 11的错误?你在以前的版本中使用过这段代码吗? –

+0

你能提供你的整个代表实现吗? – iGenio

+0

它看起来像iOS 11.2修复了这个问题 – PatrickDotStar

您的代码似乎没问题,但是在Xcode 9中出现了删除单元格的错误。如果你在Xcode 8中试用你的代码,它可能会工作。结帐this回答获取更多信息。

+0

非常感谢。但是我们的项目需要一些iOS11功能。 –

+0

@VictorChoy,你需要等到苹果解决这个问题。与此同时,您可以确保您已经下载了最新版本的Xcode 9,并希望问题很快得到解决。 –

+0

这是一个灵唯一的答案,因此是一个评论。 –

试着用我的例子。

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 
    } 
}