Pro iOS Table Views

Tim Duckett

Mentioned 2

If you’re an iOS app developer, chances are you’ll be using table views in your development projects. Table views are the bread and butter of iOS apps. With them, you can create everything from the simplest of lists to fully tricked-out user interfaces. Table views are one of the most complex components found in UIKit. While using them for boring standard user interfaces is quite simple, customizing them can become really challenging. Pro iOS Table Views takes a task-oriented focus to assist you when implementing customized table views. Although it delves deeply into the Table View API, you can always decide in which level of detail you want to dive in. It’s aimed to be a great reference and customization cookbook at the same time, useful for beginners as well as intermediate developers. Covers the entire Table View API in depth Covers customization and performance topics in depth Task-oriented reference with multiple levels of detail Note: source code for this title is currently available for download at: What you’ll learn The anatomy of tables and cells The design patterns that power the table view How to create and configure table views How to customize every last little detail of the table view How to use table views on the iPad Advanced tips and tricks for performance and killer user interfaces The latest updates in iOS 5 and how to exploit them Who this book is for The book is targeted at iOS developers who need to customize Table Views. The information is divided into several layers of detail. While the book covers complex customization topics and user interaction techniques, it provides the fundamental basics needed for customization. Both beginners and intermediate developers will find great value in this book as a helpful reference, at hand to quickly refresh their knowledge at any given time. Table of ContentsPart 1: The background and the basics Chapter 1 Table views from the ground up Chapter 2 How the table fits together Chapter 3 Feeding the table with data Part 2: Working with table content Chapter 4 How the cell fits together Chapter 5 Using tables for navigation and static content Chapter 6 Indexing table content, grouping & sorting table content: Chapter 7 Selecting & editing table content Part 3: Customizing the look and feel Chapter 8 Customizing cells: 12/15 Chapter 9 Customizing cells: 12/22 Chapter 10 Improving the cell’s interaction:12/29 Chapter 11 IPad

More on

Mentioned in questions and answers.

I have the following code examples:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) 
        [namesArray removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];        


if ([nameSection count] == 0)
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
      [tableView beginUpdates];
      [indexKeys removeObjectAtIndex:section];
      [tableView deleteSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];
      [tableView endUpdates];

Notice the order.... in the first example I am removing the object from my array first, then deleting the row from the tableview.

In the second example I am deleting the row from the tableview, then the section from my array, then the section from the tableview.

Is this the correct order to do things? Or should I be deleting it from the UI first, then the arrays?

The reason I ask is because I am getting some users who are reporting these crashes that I can't seem to repro:

NSInternalInconsistencyException - Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the

with this stack trace:

1: libobjc.A.dylib  objc_exception_throw 32
2: CoreFoundation  [NSException raise:format:] 0
3: Foundation  -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] 90
4: UIKit  -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] 6042
5: UIKit  -[UITableView _updateRowsAtIndexPaths:updateAction:withRowAnimation:] 254
6: UIKit  -[UITableView deleteRowsAtIndexPaths:withRowAnimation:] 26
7: Movies  _mh_execute_header 253319
8: UIKit -[UITableView(UITableViewInternal) animateDeletionOfRowWithCell:] 84
9: CoreFoundation  -[NSObject performSelector:withObject:withObject:] 52
10: UIKit  -[UIApplication sendAction:to:from:forEvent:] 62

After the table is in editing mode, the table view will then ask the data source whether each row should be editable. If the tableView:canEditRowAtIndexPath: method is implemented, this is called for each row in turn.

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return NO;
    return YES;

Having established whether a row can be edited, the table view then asks the delegate which editing style each row should use:

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;

When the Delete button is tapped, the tableView sends the tableView:commitEditingStyle:forRowAtIndexPath: message to the data source. It takes three parameters:

1) A reference to the tableView itself (in case the data source needs to distinguish between a number of tableViews).

2) The UITableViewCellEditingStyle of the control that’s just been tapped—in this case, UITableViewCellEditingStyleDelete

3) An indexPath object locating the row in question.

When the data source receives the commitEditingStyle:forRowAtIndexPath: message, it needs to do two things:

1) Update the tableView’s model by deleting the object represented by the row in the table. Remember that the table itself is just a view, and unless we actually delete the object from the model, it will reappear in the table the next time the table gets reloaded.

2) Send the tableView:deleteRowsAtIndexPath:withRowAnimation: message to the tableView so that it updates the table display. In this case, because we’re dealing with a Delete, it will animate the deleted cell sliding off to the left, and then move the cells below it up to close the gap.

-(void)tableView:(UITableView *)tableView 
   forRowAtIndexPath:(NSIndexPath *)indexPath {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            [self.tableData removeObjectAtIndex:indexPath.row];
            NSArray *indexPathArray = [NSArray arrayWithObject:indexPath];
            [tableView deleteRowsAtIndexPaths:indexPathArray

There’s a range of table cell insertion and deletion animations to choose from:

  1. UITableViewRowAnimationFade - Rows fade in and out.
  2. UITableViewRowAnimationRight - Inserted rows slide in from the right; deleted rows slide out to the right.
  3. UITableViewRowAnimationLeft - Inserted rows slide in from the left; deleted rows slide out to the left.
  4. UITableViewRowAnimationTop - Inserted rows slide down from the bottom of the row above; deleted rows slide up toward the bottom of the row above.
  5. UITableViewRowAnimationBottom - Inserted rows slide up from the top of the cell below; deleted rows appear to be covered by the row below sliding up.
  6. UITableViewRowAnimationNone - Inserted rows simply appear; deleted rows simply disappear.
  7. UITableViewRowAnimationMiddle - Cells are inserted and deleted with an accordion-style effect.
  8. UITableViewRowAnimationAutomatic - The tableView automatically chooses an appropriate animation style (available only in iOS 5 and later).

A great book on how to work with tables is here.

As an iOS newbie there are tons of books out there that to satisfy learning the basics. Now, I want to move over to some advanced reading such as OAuth and SQLite and dynamic API derived tableviews etc. Any resources out there that you would recommend?

Here are a couple:

More iOS 5 Development book is pretty good:

haven't gone through all of this one but it's pretty good Pro iOS Table Views: for iPhone, iPad, and iPod touch:

I know Apress goes a little deeper into other iOS topics as well, maybe check out their website... But you will probably need to rely on blogs, apple documentation, and other online sources for more advanced development