tableview trailingSwipeActionsConfigurationForRowAt crack in ios11

旧巷老猫 提交于 2019-12-01 17:34:17

问题


I Use trailingSwipeActionsConfigurationForRowAt in IOS11 but when swipe multiple then app cracked.

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let delete = UIContextualAction(style: .normal, title: "Delete") { action, view, completionHandler in
      print("Deleting!")
      completionHandler(false)
    }
    delete.backgroundColor = UIColor.red
    let config = UISwipeActionsConfiguration(actions: [delete])
    config.performsFirstActionWithFullSwipe = false
    return config
  }

and some error

 *** Assertion failure in -[UISwipeActionController swipeHandlerDidBeginSwipe:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3694.4.18/SwipeActions/UISwipeActionController.m:268
 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'No occurrence for index path (null)'

回答1:


TL;DR - look if you are doing something with your data (e.g. reloading) between your swipes.


I had similiar issue when I swipe-to-delete one cell (leaving it in edit mode - delete action visible) and then trying to swipe another caused that my app crashed. In my case it was because reloading table data in between those swipes.

Swipe-to-delete gesture calls willBeginEditingRowAtIndexPath and didEndEditingRowAtIndexPath methods on UITableViewDelegate object. In the latter one I called tableView.reloadData(). When I swipe-to-delete other cell I got didEndEditingRowAtIndexPath method called for the first cell (also reloading data) and just after that system called willBeginEditingRowAtIndexPath for the other cell and the data was out of sync and UIKit crashes.

When I removed the code that reloading data in didEndEditingRowAtIndexPath method my app won't crashes anymore :)




回答2:


You need to implement tableView(_:editActionsForRowAt:).

As a minumum, return empty array, but not nil:

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    return []
}



回答3:


I just used UIViewController + UITableViewDataSource & UITableViewDelegate combination instead of using UITableViewController for the swiping issue.



来源:https://stackoverflow.com/questions/46460022/tableview-trailingswipeactionsconfigurationforrowat-crack-in-ios11

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!