4.2. Assigning a Delegate to a Table View

Problem

You have decided to assign a delegate to a table view.

Solution

Assign an object that conforms to the UITableViewDelegate protocol to the delegate property of your table view:

- (void)viewDidLoad{
  [super viewDidLoad];

  /* We want a full-screen Table View which is as
   big as the View attached to the current
   View Controller */
  CGRect tableViewFrame = self.view.bounds;

  self.myTableView = [[UITableView alloc]
                      initWithFrame:tableViewFrame
                      style:UITableViewStylePlain];

  self.myTableView.delegate = self;

  /* Add this Table View to our View */
  [self.view addSubview:self.myTableView];

}

This code assigns the current object as the delegate of the table view. myTableView is a property of type UITableView belonging to the calling view controller. The statement is embedded in the viewDidLoad method, because the calling object here is an instance of UIViewController, and this method is the right place to put the statement so that the association is made just once.

Discussion

The UITableView class defines a property called delegate. The table view should assign to this property an object that conforms to the UITableViewDelegate protocol. In other words, this delegate must promise to reply to the messages defined in this protocol, which are sent to the delegate object by the table view itself. Think of the delegate of a table view as an object that listens to various events sent by the table view, such as when a cell is selected or when the table view wants to figure out the height of each of its cells. We can modify the visual appearance of a table and its cells (to some extent) using Interface Builder, too. Just open Interface Builder and select a table view that you previously created, and then select Tools Size Inspector. In the Size Inspector panel, you can modify the visual appearance of the table view by changing values such as the height of the table view’s cells.

To make the delegate object that you choose for a table view conform to the UITableViewDelegate protocol, you need to add that protocol to that object’s interface declaration in this way:

#import <UIKit/UIKit.h>

@interface Assigning_a_Delegate_to_a_Table_ViewViewController
           : UIViewController <UITableViewDelegate>

@property (nonatomic, strong) UITableView *myTableView;

@end

Note

It is mandatory for the delegate object to respond to messages that are marked as @required by the UITableViewDelegate protocol. Responding to other messages is optional, but the delegate must respond to any messages you want to affect the table view.

Messages sent to the delegate object of a table view carry a parameter that tells the delegate object which table view has fired that message in its delegate. This is very important to note because you might, under certain circumstances, require more than one table view to be placed on one object (usually a view). Because of this, it is highly recommended that you make your decisions based on which table view has actually sent that specific message to your delegate object, like so:

- (CGFloat)     tableView:(UITableView *)tableView
  heightForRowAtIndexPath:(NSIndexPath *)indexPath{

  CGFloat result = 20.0f;

  if ([tableView isEqual:self.myTableView]){
    result = 40.0f;
  }

  return result;

}

It is worth noting that the location of a cell in a table view is represented by its index path. An index path is the combination of the section and the row index, where the section index is the zero-based index specifying which grouping or section each cell belongs to, and the cell index is the zero-based index of that particular cell in its section.

Get iOS 6 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.