【问题标题】:How is possible indexpath.row = -1 in editActionsForRowAtIndexPath在 editActionsForRowAtIndexPath 中如何可能 indexpath.row = -1
【发布时间】:2016-10-27 11:57:59
【问题描述】:

我在我的应用程序中使用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]
}

【问题讨论】:

    标签: ios swift tableview


    【解决方案1】:

    我不确定是什么原因造成的,这可能是尝试访问不再存在的单元格的方法的问题,但一个快速、肮脏的修复方法是检查 indexPath 是否有 -1 以及它是否返回无。

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

    这至少应该阻止它在出现此 indexPath 时崩溃。

    【讨论】:

    • 然后会有-2的崩溃?更好地检查使用负值调用此函数的内容,并完全避免调用。如果是系统问题,那么制作一个示例项目并将其报告给feedbackassistant.apple.com/welcome 很重要
    猜你喜欢
    • 2020-09-26
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多