Delete a cell from a UITableView

       

Press the Edit button and delete the characters that die in Gone with the Wind. Then press Done, which calls the tableView:commitEditingStyle:forRowAtIndexPath: method of the data source. The expression @[indexPath] in this method creates an NSArray containing the indexPath.

Source code in Goner.zip

  1. main.m
  2. Class GonerAppDelegate creates a TableViewController and puts it under a UINavigationController to make the TableViewController’s navigation bar visible.
  3. Class TableViewController creates the Edit/Done button in viewDidLoad. The data source contains the method tableView:commitEditingStyle:forRowAtIndexPath:.

The view controllers

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.

Apostrophe

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

The data source and the delegate

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.

  1. numberOfSectionsInTableView:
  2. numberOfRowsInSection:
  3. tableView:cellForRowAtIndexPath: returns a UITableViewCell
  4. tableView:commitEditingStyle:forRowAtIndexPath: modifies the array and the table view. Ours deletes one cell, giving us a chance to use @[ ] 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.

  1. tableView:editingStyleForRowAtIndexPath: decides if this cell should be marked with a red minus or a green plus when the table view is in editing mode.

Things to try

  1. [Gone to the left with the wind, gone to the right with the wind.] In the tableView:commitEditingStyle:forRowAtIndexPath: method of the data source, change UITableViewRowAnimationFade to UITableViewRowAnimationLeft.

  2. [For users unfamiliar with the movie.] Display the red minus only for characters that actually die during the movie. Add the following method to the table view’s delegate. Insert it in the #pragma Table view delegate section in 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:.