【问题标题】:UITableViewRowAction completion after animation动画后 UITableViewRowAction 完成
【发布时间】:2015-04-14 07:34:07
【问题描述】:

在我的表中,我有一个 UITableViewRowAction 代表 editActionsForRowAtIndexPath。当我按下它时,它将删除我数组中的所有数据,从而触发数组上的didSet,以视图更改结束。代码如下:

var data: [Int] = [Int]() {
    didSet {
        if data.isEmpty {
            // change view
        }
    }
}

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
    var confirm = UITableViewRowAction(style: .Default, title: "Confirm") { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in
        self.data.removeAll(keepCapacity: false)
        self.tableView.setEditing(false, animated: true)
    }
    return [confirm]
}

我想要得到的是在UITableViewRowAction 的动画完成后的某种完成(行移回原位),然后清空数组并更改视图。如果可能的话,我想避免使用手动延迟。

【问题讨论】:

    标签: swift animation uitableviewrowaction


    【解决方案1】:

    试试这个代码:

    var data: [Int] = [Int]() {
        didSet {
            if data.isEmpty {
                // change view
            }
        }
    }
    
    func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
        var confirm = UITableViewRowAction(style: .Default, title: "Confirm") { (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in
            CATransaction.begin()
            CATransaction.setCompletionBlock({
                self.data.removeAll(keepCapacity: false)
            })
            self.tableView.setEditing(false, animated: true)
            CATransaction.commit()
        }
        return [confirm]
    }
    

    CATransaction.setCompletionBlock({/* completion code */}) 中的代码在CATransaction.begin()CATransaction.commit() 之间的其他代码完成执行后运行。所以这里 self.data.removeAll(keepCapacity: false) 应该在 self.tableView.setEditing(false, animated: true) 完成动画后调用。

    希望这会有所帮助!

    注意:我自己没有使用tableView.setEditing(...) 测试过这段代码,但我已经将它用于tableView.deleteRowsAtIndexPaths(...)

    【讨论】:

      猜你喜欢
      • 2020-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-11
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多