Graphics Programs Reference
In-Depth Information
Deleting Rows
In editing mode, the red circles with the dash (shown in
Figure 10.7
) are deletion controls,
and touching one should delete that row. However, at this point, touching a deletion control
doesn't 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.
@interface BNRItemStore : NSObject
{
NSMutableArray *allItems;
}
+ (BNRItemStore *)sharedStore;
- (void)removeItem:(BNRItem *)p;
In
BNRItemStore.m
, implement
removeItem:
.
- (void)removeItem:(BNRItem *)p
{
[allItems removeObjectIdenticalTo:p];
}
You could use
NSMutableArray
's
removeObject:
method here instead of
re-
moveObjectIdenticalTo:
, 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
s
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