【问题标题】:call editActionsForRowAtIndexPath manually in swift在 swift 中手动调用 editActionsForRowAtIndexPath
【发布时间】:2016-08-17 06:14:13
【问题描述】:

我想单击单元格,然后调用编辑操作。但是,我不确定如何实现手动调用滑动。我认为这个答案有解决方案,但我不太了解目标C。https://stackoverflow.com/a/29839673/5737856

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    // swipe
    //tableView(tableView, editActionsForRowAtIndexPath: indexPath) or other functions???
}

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
    let editAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "  Edit   " , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
        print("EDIT")
    })
    editAction.backgroundColor = UIColor.blueColor()

    let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
        print("DELETE")
    })
    return [deleteAction, editAction]
}

【问题讨论】:

    标签: swift uitableview swipe didselectrowatindexpath


    【解决方案1】:

    方法如下:

    var selectionIndexPath: NSIndexPath?
    
    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    
        if selectionIndexPath != nil {
    
            if selectionIndexPath!.row == indexPath.row {
    
                return true
            } else {
    
                return false
            }
    
        } else {
    
            return true
        }
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    
        selectionIndexPath = indexPath
        tableV.setEditing(true, animated: true)
    }
    
    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {
        let editAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "  Edit   " , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
            print("EDIT")
        })
        editAction.backgroundColor = UIColor.blueColor()
    
        let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
            print("DELETE")
        })
    
        let cancleAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Cancle" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
    
            self.tableV.setEditing(false, animated: true)
        })
        cancleAction.backgroundColor = UIColor.lightGrayColor()
        return [cancleAction, deleteAction, editAction]
    }
    

    你的结果将是:

    希望这会有所帮助。

    【讨论】:

    • 有没有办法直接显示删除和编辑按钮,或者把删除和编辑按钮放在左边?
    【解决方案2】:

    您可以使用此委托方法来获得滑动删除。

        func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
    
            return UITableViewCellEditingStyle.Delete
    
      }
    

    【讨论】:

    • 我希望通过单击单元格来滑动单元格并显示 EDIT 按钮和 DELETE 按钮
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-22
    • 2015-06-29
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 2016-07-18
    相关资源
    最近更新 更多