12 Apr 2014

Drops of UITableView

For "table-like" structures fans iOS offers UITableView, which is a view object, suitable to display list of items. There's also a UITableViewController that is a UIViewController, so it has a view, but a UIViewController's view is always a UITableView.

A UITableViewController can also act as the dataSource and the delegate of a UITableView.

When an instance of UITableViewController creates its view, the dataSource and the delegate "ivars" of the UITableView are automatically (not strongly) pointed at the UITableViewController instance.

Each row of a UITableView is a view, more precisely, an instance of UITableViewCell.

A UITableViewCell has the contentView property, that's the superview for the content of the cell.

A UITableViewCell  has also three other important properties: two UILabel (textLabel, detailTextLabel) and an UIImageView (imageView).

Whenever a UITableview needs to display itself it sends messages to its dataSource. The UITableViewDataSource protocol has two required methods:
tableView:numberOfRowsInSection: which returns the number of rows per section the table view should display.
tableView:cellForRowAtIndexPath: which returns the content for a cell in a determinate section and row (specified in the NSIndexPath argument).

Every table cell has a reuseIdentifier property (an NSString), that should be used to ask the table view for a reusable cell. Recyclable cells are the ones that went off screen and that should be reused in place of new UITableViewCell instances in order to save memory.
The reuseIdentifier is needed to discriminate among various type of cells, in fact UITableViewCell can be subclassed to get customised cells.

Basic "cells recycling" steps in a UITableViewController:
---> Registering a cell class:
- (void)viewDidLoad 

    [super viewDidLoad]; 
    [self.tableView registerClass:[UITableViewCell class]                                    forCellReuseIdentifier:@"UITableViewCell"];

---> Asking to pick up an "off screen reusable" cell of a certain type:
- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath 

    UITableViewCell *tableCell = 
        [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; 

    return tableCell; 

A UITableView has an editing property. A UITableViewController has it too and the controller sets the value of its table view's editing property to match its own one.

These were just a few words about the table views in iOS, but there's a lot more to find out and learn about.