How do I use undo functionality for Tableview Data source

I am trying to build undo functionality. I am using NSMutableArray as datasource for my TableView. If I do change the value in the table view and want to revert back the old value, how do I achieve it?

Suppose my initial NSMutable array elements are 1,2,3,4,5 and in table view if I change the value at row 1(assuming rows are zero indexed) from 2 to 10, my array elements become 1,10,3,4,5. How can I revert back to 1,2,3,4,5 in my array and reflect the same in table.

hi @vivekstorm,
the first simplest way that comes to mind is to create an array that holds the difference, this can be either an insert or a remove. So when you undo, you simply do the opposite and that way you will have the last state of that tableview.

There is also an NSUndoManager that you can look into for more details.


1 Like

I second the idea of using NSUndoManager, think of it as a stack based setup where you can Undo/Redo semi-unlimited times. Some good resources:

NSUndoManager at NSHipster UndoManager with Swift (iOS, but concepts apply)

1 Like

Hi @jayantvarma Thanks for the help. I tried with array that holds difference and It works!

Hi @vivekstorm, you are welcome

This topic was automatically closed after 166 days. New replies are no longer allowed.