简体   繁体   English

通过尝试删除,对UITableViewCell的自定义编辑操作在滑动时崩溃

[英]Custom edit action on UITableViewCell crashes on swipe by trying to delete

I added a custom swipe to share action on my UITableViewCells in my UITableView. 我在UITableView中添加了自定义滑动以共享对UITableViewCells的操作。 My problem is that for some reason it won't even let me swipe the cell. 我的问题是,由于某种原因,它甚至不让我滑动单元格。 It immediately crashes with this line: 它立即与此行崩溃:

reason: 'attempt to delete row 25 from section 0 which only contains 25 rows before the update' 原因:“试图从第0节中删除第25行,该行仅包含更新前的25行”

However, I'm not trying to delete anything! 但是,我不是要删除任何东西! It's a share button not a delete button. 这是一个共享按钮,而不是删除按钮。 Here are the relevant methods I've added: 这是我添加的相关方法:

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    let shareAction = UITableViewRowAction(style: .Normal, title: "Share") { (action: UITableViewRowAction, indexPath: NSIndexPath) -> Void in
        // Some stuff
    }
    shareAction.backgroundColor = UIColor.yellowColor()
    return [shareAction]
}

override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
}

Why is my table view controller giving me this error when it should have no business attempting to delete anything? 为什么我的表视图控制器在没有业务尝试删除任何内容时会给我这个错误?

It turns out it was an issue unrelated to the three methods above. 事实证明,这是与上述三种方法无关的问题。 My table view controller is a PFQueryTableViewController subclass which has numberOfRowsInSection baked in out of the box. 我的表视图控制器是一个PFQueryTableViewController子类,该子类具有开箱即用的numberOfRowsInSection My problem was that I explicitly added that method when I shouldn't have. 我的问题是我在不应该使用的时候明确添加了该方法。 Therefore it was being called when it shouldn't have been (it's build into the subclass implicitly), resulting in some wonky behavior. 因此,它在不应该被调用时被调用(它隐式地内置在子类中),从而导致一些奇怪的行为。 I removed my call to numberOfRowsInSection and it worked as it should. 我删除了对numberOfRowsInSection调用,它可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM