Undo manager for tableView row delete

江枫思渺然 提交于 2019-12-12 03:43:25

问题


Is it possible to implement an undo manager when you delete a row in a tableView?

I have read about undo manager and you have to set the viewController as becomeFirstResponder().

The problem is that I am using a tableView inside a collectionViewCell

How shall I proceed with registering an undo?

I tried the following and it doesn't seem to work

    @IBAction func tableButtonUndo(_ sender: Any) {

        print("undo working")
        undoItem()

    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        indexPathSelected = indexPath.row

        if tableView == ordersTableView {

            print("tap in kitchen table view")


            self.removeItemKitchen()

            ordersTableView.reloadData()

        }
    }

    func removeItemKitchen() {
        orderSingle.orderListFormatted.remove(at: indexPathSelected)
        undoManager?.registerUndo(withTarget: MyVC(), selector: #selector(self.undoItem), object: nil)
        undoManager?.setActionName("Undo")
    }


    func undoItem() {
        undoManager?.registerUndo(withTarget: MyVC(), selector: #selector(self.removeItemKitchen), object: nil)
        undoManager?.setActionName("Undo")
        undoManager?.undo()
    }

This code should undo the operation, but instead, I get a redo, it deletes the next row.

I would like to recover the deleted row by tapping the undo button

来源:https://stackoverflow.com/questions/41623854/undo-manager-for-tableview-row-delete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!