无法在 UITableView 编辑模式下删除按钮

Unable to Remove Buttons in UITableView Edit Mode

当我的 UITableView 处于编辑模式并且我按下删除按钮 (-) 时,我隐藏行中的编辑按钮并显示删除和取消按钮。

如果按下“取消”按钮我没问题,因为它会触发按钮的“取消”处理程序并且我会重新显示“编辑”按钮。

但是,如果用户在行内按下(而不是“取消”或“删除”按钮),则该行退出删除模式,不允许我显示“编辑”按钮。

在删除模式退出时或在删除模式下按下行时,我可以检测到某些事件吗?

func tableView(tableView: UITableView!, editActionsForRowAtIndexPath indexPath: NSIndexPath!) -> [AnyObject]!
{
// hide the Edit button in this row
self.editButtons[indexPath.row].hidden = true

// create a Cancel button to abort delete
var cancelAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Cancel", handler:  
{( 
    action: UITableViewRowAction!, indexPath: NSIndexPath!) in

// if Cancel pressed, show the Edit button again 
self.editButtons[indexPath.row].hidden = false

// reload the row to get rid of the Delete and Cancel buttons   
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)

return
})

var deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete",handler:
{( 
action: UITableViewRowAction!, indexPath: NSIndexPath!) in
    println("Delete not implemented")
    return
})
return [deleteAction, cancelAction]
}

您可以覆盖 UITableViewCell 中的 willTransitionToState 函数以跟踪状态变化。 您必须跟踪 previousState,以检查您的删除按钮是否被隐藏。

class CustomCell : UITableViewCell
{
    var previousState : UITableViewCellStateMask = UITableViewCellStateMask.allZeros

    override func willTransitionToState(state: UITableViewCellStateMask) {
        if state & UITableViewCellStateMask.ShowingEditControlMask != nil
        {
            if previousState & UITableViewCellStateMask.ShowingDeleteConfirmationMask != nil
            {
                println("hiding delete")
                /* Send a notification or call a function by setting a 
                   delegate from the view controller.*/
            }

        }
        previousState = state
    }
}