【问题标题】:Unable to Remove Buttons in UITableView Edit Mode在 UITableView 编辑模式下无法删除按钮
【发布时间】:2015-01-22 00:01:51
【问题描述】:

当我的 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]
}

【问题讨论】:

    标签: uitableview swift


    【解决方案1】:

    您可以覆盖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
        }
    }
    

    【讨论】:

    • 这看起来是正确的答案。当我退出编辑模式时,它正在触发该功能。但是我是 Swift 新手,需要弄清楚如何在视图控制器中设置和调用委托。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多