如何在editActionsForRowAtIndexPath中使用indexpath.row = -1

问题描述 投票:2回答:1

我正在我的应用程序中使用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个回答
0
投票
我不确定是什么原因造成的,可能是试图访问一个不再存在的单元格的方法出现问题,但是一个快速,肮脏的解决方法是仅检查indexPath是否为-1以及是否返回nil 。

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

此索引路径出现时,这至少应使其停止崩溃。
© www.soinside.com 2019 - 2024. All rights reserved.