Press the Edit
button
and delete the characters that die in
Gone
with the Wind.
Then press Done,
which calls the
tableView:commitEditingStyle:forRowAtIndexPath:
@[indexPath]
in this method creates an
NSArray
containing the
indexPath
.
main.m
GonerAppDelegate
creates a
TableViewController
and puts it under a
UINavigationController
to make the
TableViewController
’s
navigation bar visible.TableViewController
creates the Edit/Done button in
viewDidLoad
.
The data source contains the method
tableView:commitEditingStyle:forRowAtIndexPath:
.To display the brushed aluminum navigation bar at the top of the window, we have to put a view controller above the table view, and a navigation controller above the view controller. The view controller immediately above a table view must be a subclass of table view controller. The table view controller will act as the table view’s data source and delegate. It wears three hats: view controller, data source, and delegate.
Unless we say otherwise
(by giving a
tableView:editingStyleForRowAtIndexPath:
method to the
table
view delegate),
each cell will get a
red
minus
rather than a
green
plus
or nothing at all.
Scarlett O’Hara
looks better with an apostrophe
(Unicode
\u2019
),
than with a single quote
(Unicode
\u0039
).
I’ll make them bigger so you can see the difference:
O’Hara (apostrophe)
O'Hara (single quote)
Edit → Special Characters… → Punctuation
Our
TableViewController
serves as
the table view’s data source
and the table view’s delegate.
Since it is the data source,
it contains an array and the following methods.
numberOfSectionsInTableView:
numberOfRowsInSection:
tableView:cellForRowAtIndexPath:
returns a
UITableViewCell
tableView:commitEditingStyle:forRowAtIndexPath:
@[ ]
to create an array containing only one object.
Since the
TableViewController
is the table view’s delegate,
it can also have the following method.
We didn’t bother to write the method,
since we’re satisfied with the version of the method that
TableViewController
inherits from
UITableViewController
.
tableView:editingStyleForRowAtIndexPath:
tableView:commitEditingStyle:forRowAtIndexPath:
UITableViewRowAnimationFade
to
UITableViewRowAnimationLeft
.
#pragma Table view delegate
TableViewwController.m
- (UITableViewCellEditingStyle) tableView: (UITableView *) tableView editingStyleForRowAtIndexPath: (NSIndexPath *) indexPath { NSSet *doomed = [NSSet setWithObjects: @"Melanie Hamilton", @"Scarlett’s father", @"Scarlett’s mother", @"Charles Hamilton (1st husband)", @"Frank Kennedy (2nd husband)", @"Bonnie Butler (S’s daughter)", nil ]; UITableViewCell *cell = [tableView cellForRowAtIndexPath: indexPath]; NSString *text = cell.textLabel.text; if ([doomed containsObject: text]) { return UITableViewCellEditingStyleDelete; } return UITableViewCellEditingStyleNone; }
Better yet,
let
doomed
be an instance variable of class
TableViewController
so we don’t have to re-create it every time we call
tableView:editingStyleForRowAtIndexPath:
.