ios - 在editActionsForRowAtIndexPath中如何indexpath.row = -1

标签 ios swift tableview

我在我的应用程序中使用editActionsForRowAtIndexPath进行删除和编辑按钮,但是如果我又一次又一次地滑动,那么一次我收到indexpath.row = -1,这让我感到震惊,但是我有检查并访问了更多演示项目得到相同的indexpath.row = -1
所以我该如何处理,就像我希望在这种情况下无法滑动一样。

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    print(indexPath.row)
    //Edit
    let editAction = UITableViewRowAction(style: .Normal, title: "Edit", handler: {
        action in
    })
    editAction.backgroundColor = UIColor(red: 212.0/255, green: 37.0/255, blue: 37.0/255, alpha: 1)

    //Delete
    let deleteAction = UITableViewRowAction(style: .Normal, title: "Delete", handler: {
        action in
    })
    deleteAction.backgroundColor = UIColor(red: 183.0/255, green: 27.0/255, blue: 27.0/255, alpha: 1)
    return [deleteAction,editAction]
}

最佳答案

我不确定是什么原因造成的,可能是尝试访问一个不再存在的单元格的方法出现问题,但是一个快速,肮脏的解决方法是仅检查indexPath是否为-1以及它是否返回nil。

if(indexPath.section != -1 && indexPath.row != -1) {
    //Code for creating actions
}
return nil

当此indexPath出现时,这至少应阻止它崩溃。

关于ios - 在editActionsForRowAtIndexPath中如何indexpath.row = -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40284025/

相关文章:

ios - 检索核心数据关系的计数

iPhone 拉动以刷新移动图标位置

swift - 如何在 swift UITests 的 XCUIApplication 中设置暗模式?

ios - 当我的应用程序关闭时,解析服务器实时查询确实收到了新对象吗?

ios - 当我点击其中一个通知时,所有通知都被隐藏

ios - 设置标签自定义控件 ?

ios - 如何处理同一地址上的多个位置并将它们聚类?

ios - 将字符串查询解析为数组

ios - TableView 数据被覆盖

ios - 在不同的表格 View 单元格中显示同一行中的数据