首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在iOS11中快速连续滑动删除UITableView的单元格会崩溃

在iOS11中快速连续滑动删除UITableView的单元格会崩溃
EN

Stack Overflow用户
提问于 2017-08-29 10:54:43
回答 2查看 2.4K关注 0票数 20

问题就像标题一样。这是我的代码。我发现从右向左滑动直接删除的操作是iOS11中的新功能

代码语言:javascript
复制
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()

这是崩溃日志。

代码语言:javascript
复制
*** 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
......
EN

回答 2

Stack Overflow用户

发布于 2017-08-29 11:00:30

您的代码看起来没问题,但是Xcode9中有一个删除单元格的bug。如果您在Xcode8中尝试您的代码,它可能会工作。查看this answer了解更多信息。

票数 3
EN

Stack Overflow用户

发布于 2017-09-12 21:24:58

试着用我的例子。

代码语言:javascript
复制
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
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45930031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档