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