Deleting Rows

In editing mode, the red circles with the dash (shown in Figure 11.7) are deletion controls, and touching one should delete that row. However, at this point, touching a deletion control doesn’t delete anything. (Try it and see.) Before the table view will delete a row, it sends its data source a message about the deletion and waits for confirmation before pulling the trigger.

A UITableView asks its data source for the cells it should display when it is first loaded and at least three other times:

  • when the user scrolls the table view
  • when the table view is removed from the view hierarchy and then added back to the view hierarchy
  • when an object sends it the message reloadData

Now consider what would happen if deleting a row only removed the row from the table view and not from the data source. The PossessionStore would still have the Possession instance displayed by that row, and the next time the UITableView asked for its rows, the data source would create a cell for the supposedly deleted Possession. The unwanted row would rise from the dead and return to the table.

To prevent zombie cells from roaming your table, you must update your data source to remove objects that should no longer be displayed in the table. First, we need a way to remove objects from the PossessionStore. In PossessionStore.h, add a new method that deletes a Possession.

@​i​n​t​e​r​f​a​c​e​ ​P​o​s​s​e​s​s​i​o​n​S​t​o​r​e​ ​:​ ​N​S​O​b​j​e​c​t​
{​
 ​ ​ ​ ​N​S​M​u​t​a​b​l​e​A​r​r​a​y​ ​*​a​l​l​P​o​s​s​e​s​s​i​o​n​s​;​
}​
+​ ​(​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​;​

-​ ​(​v​o​i​d​)​r​e​m​o​v​e​P​o​s​s​e​s​s​i​o​n​:​(​P​o​s​s​e​s​s​i​o​n​ ​*​)​p​;​

In PossessionStore.m, implement removePossession:.

-​ ​(​v​o​i​d​)​r​e​m​o​v​e​P​o​s​s​e​s​s​i​o​n​:​(​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​ ​r​e​m​o​v​e​O​b​j​e​c​t​I​d​e​n​t​i​c​a​l​T​o​:​p​]​;​
}​

Now you will implement tableView:​commitEditingStyle:​forRowAtIndexPath:, a method from the UITableViewDataSource protocol. (This message is sent to the ItemsViewController. Keep in mind that while the PossessionStore is the where the data is kept, the ItemsViewController is the table view’s data source.)

When tableView:​commitEditingStyle:​forRowAtIndexPath: is sent to the data source, two extra arguments are passed along with it. The first is the UITableViewCellEditingStyle, which, in this case, is UITableViewCellEditingStyleDelete. The other argument is the NSIndexPath of the row in the table. In ItemsViewController.m, implement this method to have the PossessionStore remove the right object and to confirm the row deletion by sending the message deleteRowsAtIndexPaths:withRowAnimation: back to the table view.

-​ ​(​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​
 ​ ​ ​ ​c​o​m​m​i​t​E​d​i​t​i​n​g​S​t​y​l​e​:​(​U​I​T​a​b​l​e​V​i​e​w​C​e​l​l​E​d​i​t​i​n​g​S​t​y​l​e​)​e​d​i​t​i​n​g​S​t​y​l​e​
 ​ ​ ​ ​ ​f​o​r​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​ ​*​)​i​n​d​e​x​P​a​t​h​
{​
 ​ ​ ​ ​/​/​ ​I​f​ ​t​h​e​ ​t​a​b​l​e​ ​v​i​e​w​ ​i​s​ ​a​s​k​i​n​g​ ​t​o​ ​c​o​m​m​i​t​ ​a​ ​d​e​l​e​t​e​ ​c​o​m​m​a​n​d​.​.​.​
 ​ ​ ​ ​i​f​ ​(​e​d​i​t​i​n​g​S​t​y​l​e​ ​=​=​ ​U​I​T​a​b​l​e​V​i​e​w​C​e​l​l​E​d​i​t​i​n​g​S​t​y​l​e​D​e​l​e​t​e​)​
 ​ ​ ​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​P​o​s​s​e​s​s​i​o​n​S​t​o​r​e​ ​*​p​s​ ​=​ ​[​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​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​A​r​r​a​y​ ​*​p​o​s​s​e​s​s​i​o​n​s​ ​=​ ​[​p​s​ ​a​l​l​P​o​s​s​e​s​s​i​o​n​s​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​P​o​s​s​e​s​s​i​o​n​ ​*​p​ ​=​ ​[​p​o​s​s​e​s​s​i​o​n​s​ ​o​b​j​e​c​t​A​t​I​n​d​e​x​:​[​i​n​d​e​x​P​a​t​h​ ​r​o​w​]​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​p​s​ ​r​e​m​o​v​e​P​o​s​s​e​s​s​i​o​n​:​p​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​W​e​ ​a​l​s​o​ ​r​e​m​o​v​e​ ​t​h​a​t​ ​r​o​w​ ​f​r​o​m​ ​t​h​e​ ​t​a​b​l​e​ ​v​i​e​w​ ​w​i​t​h​ ​a​n​ ​a​n​i​m​a​t​i​o​n​
 ​ ​ ​ ​ ​ ​ ​ ​[​t​a​b​l​e​V​i​e​w​ ​d​e​l​e​t​e​R​o​w​s​A​t​I​n​d​e​x​P​a​t​h​s​:​[​N​S​A​r​r​a​y​ ​a​r​r​a​y​W​i​t​h​O​b​j​e​c​t​:​i​n​d​e​x​P​a​t​h​]​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​w​i​t​h​R​o​w​A​n​i​m​a​t​i​o​n​:​Y​E​S​]​;​
 ​ ​ ​ ​}​
}​

Build and run your application and then delete a row. It will disappear. Now scroll the list to force the table view to reload. Then return to where the deleted row was and confirm that your data source was updated.

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

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