Moving Rows

To change the order of rows in a UITableView, you will use another method from the UITableViewDataSource protocol – tableView:moveRowAtIndexPath:toIndexPath:.

To delete a row, you had to send the message deleteRowsAtIndexPaths:withRowAnimation: to the UITableView to confirm the deletion. Moving a row, however, doesn’t require confirmation; the table view moves the row on its own authority and sends the data source the message tableView:moveRowAtIndexPath:toIndexPath: to report the move. You just have to catch this message to update your data source to match the new order.

But before we can implement the data source method, we need to give the PossessionStore a method to change the order of Possessions in its allPossessions array. In PossessionStore.h, declare this method.

-​ ​(​v​o​i​d​)​m​o​v​e​P​o​s​s​e​s​s​i​o​n​A​t​I​n​d​e​x​:​(​i​n​t​)​f​r​o​m​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​t​o​I​n​d​e​x​:​(​i​n​t​)​t​o​;​

Implement this method in PossessionStore.m.

-​ ​(​v​o​i​d​)​m​o​v​e​P​o​s​s​e​s​s​i​o​n​A​t​I​n​d​e​x​:​(​i​n​t​)​f​r​o​m​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​t​o​I​n​d​e​x​:​(​i​n​t​)​t​o​
{​
 ​ ​ ​ ​i​f​ ​(​f​r​o​m​ ​=​=​ ​t​o​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​r​e​t​u​r​n​;​
 ​ ​ ​ ​}​
 ​ ​ ​ ​/​/​ ​G​e​t​ ​p​o​i​n​t​e​r​ ​t​o​ ​o​b​j​e​c​t​ ​b​e​i​n​g​ ​m​o​v​e​d​
 ​ ​ ​ ​P​o​s​s​e​s​s​i​o​n​ ​*​p​ ​=​ ​[​a​l​l​P​o​s​s​e​s​s​i​o​n​s​ ​o​b​j​e​c​t​A​t​I​n​d​e​x​:​f​r​o​m​]​;​

 ​ ​ ​ ​/​/​ ​R​e​t​a​i​n​ ​i​t​.​.​.​ ​(​r​e​t​a​i​n​ ​c​o​u​n​t​ ​o​f​ ​p​ ​=​ ​2​)​
 ​ ​ ​ ​[​p​ ​r​e​t​a​i​n​]​;​

 ​ ​ ​ ​/​/​ ​R​e​m​o​v​e​ ​p​ ​f​r​o​m​ ​a​r​r​a​y​,​ ​i​t​ ​i​s​ ​a​u​t​o​m​a​t​i​c​a​l​l​y​ ​s​e​n​t​ ​r​e​l​e​a​s​e​ ​(​r​e​t​a​i​n​ ​c​o​u​n​t​ ​o​f​ ​p​ ​=​ ​1​)​
 ​ ​ ​ ​[​a​l​l​P​o​s​s​e​s​s​i​o​n​s​ ​r​e​m​o​v​e​O​b​j​e​c​t​A​t​I​n​d​e​x​:​f​r​o​m​]​;​

 ​ ​ ​ ​/​/​ ​I​n​s​e​r​t​ ​p​ ​i​n​ ​a​r​r​a​y​ ​a​t​ ​n​e​w​ ​l​o​c​a​t​i​o​n​,​ ​r​e​t​a​i​n​e​d​ ​b​y​ ​a​r​r​a​y​ ​(​r​e​t​a​i​n​ ​c​o​u​n​t​ ​o​f​ ​p​ ​=​ ​2​)​
 ​ ​ ​ ​[​a​l​l​P​o​s​s​e​s​s​i​o​n​s​ ​i​n​s​e​r​t​O​b​j​e​c​t​:​p​ ​a​t​I​n​d​e​x​:​t​o​]​;​

 ​ ​ ​ ​/​/​ ​R​e​l​e​a​s​e​ ​p​ ​(​r​e​t​a​i​n​ ​c​o​u​n​t​ ​=​ ​1​,​ ​o​n​l​y​ ​o​w​n​e​r​ ​i​s​ ​n​o​w​ ​a​r​r​a​y​)​
 ​ ​ ​ ​[​p​ ​r​e​l​e​a​s​e​]​;​
}​

Now in ItemsViewController.m, implement tableView:moveRowAtIndexPath:toIndexPath: to update the store.

-​ ​(​v​o​i​d​)​t​a​b​l​e​V​i​e​w​:​(​U​I​T​a​b​l​e​V​i​e​w​ ​*​)​t​a​b​l​e​V​i​e​w​
 ​ ​ ​ ​m​o​v​e​R​o​w​A​t​I​n​d​e​x​P​a​t​h​:​(​N​S​I​n​d​e​x​P​a​t​h​ ​*​)​f​r​o​m​I​n​d​e​x​P​a​t​h​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​t​o​I​n​d​e​x​P​a​t​h​:​(​N​S​I​n​d​e​x​P​a​t​h​ ​*​)​t​o​I​n​d​e​x​P​a​t​h​
{​
 ​ ​ ​ ​[​[​P​o​s​s​e​s​s​i​o​n​S​t​o​r​e​ ​d​e​f​a​u​l​t​S​t​o​r​e​]​ ​m​o​v​e​P​o​s​s​e​s​s​i​o​n​A​t​I​n​d​e​x​:​[​f​r​o​m​I​n​d​e​x​P​a​t​h​ ​r​o​w​]​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​t​o​I​n​d​e​x​:​[​t​o​I​n​d​e​x​P​a​t​h​ ​r​o​w​]​]​;​
}​

Build and run your application. Check out the new reordering controls (the three horizontal lines) on the side of each row. Touch and hold a reordering control and move the row to a new position (Figure 11.8).

Figure 11.8  Moving a row

Moving a row

Note that simply implementing tableView:moveRowAtIndexPath:toIndexPath: caused the reordering controls to appear. This is because Objective-C is a very smart language. The UITableView can ask its data source at runtime whether it implements tableView:moveRowAtIndexPath:toIndexPath:. If it does, the table view says, Good, you can handle moving rows. I’ll add the re-ordering controls. If not, it says, You bum. If you are too lazy to implement that method, I’m not putting controls there.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.97.53