User Tools

Site Tools


ios-labs-s14:basics-tables

UITableView

An extremely common, and slightly more complex, view used to build interfaces.

Displays a set of data in cells, which the developer sets the number of. Allows a user to scroll through cells vertically.

Requires 3 pieces of information to function: the number of sections in the table, number of cells in each section, and the contents of each cell.

Relies on delegation to communicate with a controller and determine functionality based on the above. Also uses delegation to alert a controller when a user touches a cell.

The delegate methods required by a UITableView:

  • func tableView(tableView: UITableView, numberOfRowsInSection section: Int) → Int

Return integers to the table.

  • func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) → UITableViewCell

Method receives a cell that is dequeued from memory (has been scrolled off the screen and is no longer visible) for editing. Method is called when the table needs to display a new cell. Developer must change the views within the cell to represent model information, commonly using indexPath.row as an index into an array. Cell is returned at the end of the method, at which point the table loads it into memory and onto the screen.

Note that cells have a property called reuse identifier, a string used to identify types of cells in the case of multiple tables. Set this property using the Attributes Inspector of InterfaceBuilder, access it in the cellForRowAtIndexPath method.

  • func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

Called when a user touches a cell. Includes the index of the touched cell in indexPath.row.

ios-labs-s14/basics-tables.txt · Last modified: 2015/10/30 11:47 by mbarboi