In this tutorial, I will show you how to move rows.
Start by creating a new project by selecting “Navigation-Based Application”. The first thing we do is create a data source which will be used by the table view. The data source need not be complex but a simple class called “SampleData” with an integer and a string. This is how the header file looks like
Header File Here – SampleData
We then create a mutable array in the header file of the root view controller and release its memory in the dealloc method. Uncomment viewDidLoad method and inistialize the array and add at-least 5 object to it. This is how my implementation file looks like
Implementation File – View Did Load
We initialized the array and added 5 objects to it. We then change the count returned by “numberOfRowsInSection” to the count of the array, in our case 5. cellForRowAtIndexPath is the method which is called when a table view wants to add data to a table view cell. We get the right object from the array and set the text property of the cell. The final line is what adds the “Edit” button to the right bar button item.
Implementation File – cellForRowAtIndexPath
Run your application in the simulator and you should see 5 rows in the table view. We cannot move the rows yet, to be able to do that we need to uncomment “moveRowAtIndexPath” method. Test your application now and you should be able to move rows up and down.
Now we need some way to persist the data, from our SampleData class we have a field called order which is what we will use to reset the order.