Deleting Rows

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

When deleting a cell, you must do two things: remove the row from the UITableView and remove the BNRItem associated with it from the BNRItemStore. To pull this off, the BNRItemStore must know how to remove objects from itself.

In BNRItemStore.h, declare a new method.

@​i​n​t​e​r​f​a​c​e​ ​B​N​R​I​t​e​m​S​t​o​r​e​ ​:​ ​N​S​O​b​j​e​c​t​
+​ ​(​B​N​R​I​t​e​m​S​t​o​r​e​ ​*​)​s​h​a​r​e​d​S​t​o​r​e​;​

@​p​r​o​p​e​r​t​y​ ​(​n​o​n​a​t​o​m​i​c​,​ ​s​t​r​o​n​g​,​ ​r​e​a​d​o​n​l​y​)​ ​N​S​A​r​r​a​y​ ​*​a​l​l​I​t​e​m​s​;​

-​ ​(​B​N​R​I​t​e​m​ ​*​)​c​r​e​a​t​e​I​t​e​m​;​
-​ ​(​v​o​i​d​)​r​e​m​o​v​e​I​t​e​m​:​(​B​N​R​I​t​e​m​ ​*​)​i​t​e​m​;​

@​e​n​d​

In BNRItemStore.m, implement removeItem:.

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

You could use NSMutableArray’s removeObject: method here instead of removeObjectIdenticalTo:, but consider the difference: removeObject: goes to each object in the array and sends it the message isEqual:. A class can implement this method to return YES or NO based on its own determination. For example, two BNRItem objects could be considered equal if they had the same valueInDollars.

The method removeObjectIdenticalTo:, on the other hand, removes an object if and only if it is the exact same object as the one passed in this message. While BNRItem does not currently override isEqual: to do special checking, it could in the future. Therefore, you should use removeObjectIdenticalTo: when you are specifying a particular instance.

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

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 BNRItemsViewController.m, implement this method to have the BNRItemStore 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​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​A​r​r​a​y​ ​*​i​t​e​m​s​ ​=​ ​[​[​B​N​R​I​t​e​m​S​t​o​r​e​ ​s​h​a​r​e​d​S​t​o​r​e​]​ ​a​l​l​I​t​e​m​s​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​B​N​R​I​t​e​m​ ​*​i​t​e​m​ ​=​ ​i​t​e​m​s​[​i​n​d​e​x​P​a​t​h​.​r​o​w​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​[​[​B​N​R​I​t​e​m​S​t​o​r​e​ ​s​h​a​r​e​d​S​t​o​r​e​]​ ​r​e​m​o​v​e​I​t​e​m​:​i​t​e​m​]​;​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​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​:​@​[​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​:​U​I​T​a​b​l​e​V​i​e​w​R​o​w​A​n​i​m​a​t​i​o​n​F​a​d​e​]​;​
 ​ ​ ​ ​}​
}​

Build and run your application, create some rows, and then delete a row. It will disappear. Notice that swipe-to-delete works also.

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

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