2

私のテーブルには、UITableViewRowActionforがあり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です(行がその場所に戻ります)。その後、配列を空にしてビューを変更します。可能であれば、手動遅延の使用を避けたいと思います。

4

1 に答える 1

3

このコードを試してください:

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(...).

于 2015-08-03T02:15:50.660 に答える