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, does not require confirmation; the table view moves the row on its own authority and reports the move to its the data source by sending the message tableView:moveRowAtIndexPath:toIndexPath:. You just have to implement this method to update your data source to match the new order.

But before you can implement the data source method, you need to give the BNRItemStore a method to change the order of items in its allItems array. In BNRItemStore.h, declare this method.

-​ ​(​v​o​i​d​)​m​o​v​e​I​t​e​m​A​t​I​n​d​e​x​:​(​N​S​U​I​n​t​e​g​e​r​)​f​r​o​m​I​n​d​e​x​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​t​o​I​n​d​e​x​:​(​N​S​U​I​n​t​e​g​e​r​)​t​o​I​n​d​e​x​;​

In BNRItemStore.m, implement moveItemAtIndex:toIndex:.

-​ ​(​v​o​i​d​)​m​o​v​e​I​t​e​m​A​t​I​n​d​e​x​:​(​N​S​U​I​n​t​e​g​e​r​)​f​r​o​m​I​n​d​e​x​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​t​o​I​n​d​e​x​:​(​N​S​U​I​n​t​e​g​e​r​)​t​o​I​n​d​e​x​
{​
 ​ ​ ​ ​i​f​ ​(​f​r​o​m​I​n​d​e​x​ ​=​=​ ​t​o​I​n​d​e​x​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​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​ ​s​o​ ​y​o​u​ ​c​a​n​ ​r​e​-​i​n​s​e​r​t​ ​i​t​
 ​ ​ ​ ​B​N​R​I​t​e​m​ ​*​i​t​e​m​ ​=​ ​s​e​l​f​.​p​r​i​v​a​t​e​I​t​e​m​s​[​f​r​o​m​I​n​d​e​x​]​;​

 ​ ​ ​ ​/​/​ ​R​e​m​o​v​e​ ​i​t​e​m​ ​f​r​o​m​ ​a​r​r​a​y​
 ​ ​ ​ ​[​s​e​l​f​.​p​r​i​v​a​t​e​I​t​e​m​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​d​e​x​]​;​

 ​ ​ ​ ​/​/​ ​I​n​s​e​r​t​ ​i​t​e​m​ ​i​n​ ​a​r​r​a​y​ ​a​t​ ​n​e​w​ ​l​o​c​a​t​i​o​n​
 ​ ​ ​ ​[​s​e​l​f​.​p​r​i​v​a​t​e​I​t​e​m​s​ ​i​n​s​e​r​t​O​b​j​e​c​t​:​i​t​e​m​ ​a​t​I​n​d​e​x​:​t​o​I​n​d​e​x​]​;​
}​

In BNRItemsViewController.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​ ​*​)​s​o​u​r​c​e​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​ ​*​)​d​e​s​t​i​n​a​t​i​o​n​I​n​d​e​x​P​a​t​h​
{​
 ​ ​ ​ ​[​[​B​N​R​I​t​e​m​S​t​o​r​e​ ​s​h​a​r​e​d​S​t​o​r​e​]​ ​m​o​v​e​I​t​e​m​A​t​I​n​d​e​x​:​s​o​u​r​c​e​I​n​d​e​x​P​a​t​h​.​r​o​w​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​t​o​I​n​d​e​x​:​d​e​s​t​i​n​a​t​i​o​n​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 9.9).

Figure 9.9  Moving a row

Moving a row

Note that simply implementing tableView:moveRowAtIndexPath:toIndexPath: caused the reordering controls to appear. 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, If you aren’t implementing that method, then I won’t put controls there.

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

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