Skip to content

Table views basics

In this tutorial we cover the following topics

  • 1. General information
  • 2. Introduction
    1. Step 1: create a new project
    2. Step 2: add Table view and set connection
    3. Step 3: add a code
    4. Step 4: using table view cell styles
    5. Step 5: add an image
    6. Step 6: handling row selection
    7. Step 7: disable row selection
    8. Step 8: delete TableView row when swipe
  • 3. Customizing Table View Cells with nib files
    1. Step 1: create a new project
    2. Step 2: create a new class
    3. Step 3: add a new code and a table view
    4. Step 4: design our table view cell in Interface Builder
    5. Step 5: run the application
  • 4. Customizing Table View Cells with storyboard
    1. Step 1: create a new project
    2. Step 2: create a new class
    3. Step 3: add a new code
    4. Step 4: add a table view and design our table view cell in the storyboard
    5. Step 5: run the application


General information

Technically, a table view is the view object that displays a table’s data which is an instance of the class UITableView. Each visible row in a table is implemented by an instance of the UITableViewCell class. Table views are not responsible for storing table’s data. They store only enough data to draw the rows that are currently visible. Table views get their configuration data from an object that conforms to the UITableViewDelegate protocol and their row data from an object that conforms to the UITableViewDataSource protocol.

We can also put more data, than standatd title and details one-line strings, in a cell if we need to by adding subviews to UITableViewCell. We do this using either by adding subviews programmatically when creating the cell or by loading them from a storyboard or nib file.

Table views come in two basic styles

  • Grouped A grouped table view contains one or more sections of rows. Within each section, all rows sit tightly together in a nice little group; but between sections, there are clearly visible gaps.
  • Plain This is the default style. In this style, the sections are slightly closer together, and each section’s header can optionally be styled in a custom manner. When an index is used, this style is also referred to as indexed.
2. Introduction
  1. Step 1: create a new project
    Create a new project

    1. Select File | New | Project...
    2. Select Single View Application on the template selection sheet.
    3. As the product name enter iOS Table View Basic
    4. Set the Language to Objective-C, devices pop-up button to Universal and make sure the check box labeled Use Core Data is unchecked.
  2. Step 2: add Table view and set connections
    1. Select Main.storyboard to edit the storyboard.
    2. Find in the object library a Table View and drag it over to the View window.
    3. Add all necessary constraints to make sure that the table view is positioned and sized correctly.
    4. Select the table view in the Document Inspector and bring up the Connections Inspector (use shortcut: Alt + Command + 6).
    5. Drag from the circle next to dataSource and delegate in Outlets section to the View Controller icon in the Document Outline or above the view controller in the storyboard editor. This makes our controller class both the data source and delegate for this table.
  3. Step 3: add a code
    1. Single-click ViewController.m and add the following code

  4. Step 4: using table view cell styles
    Except the default UITableViewCellStyleDefault the UITableViewCell class includes several other predefined cell styles that let us easily add a bit more variety to our table views. These cell styles use three different cell elements

    • Text label This is the cell’s main text. In the case of the default style UITableViewCellStyleDefault the text label is the only text shown in the cell.
    • Detail text label This is the cell’s secondary text, usually used as an explanatory note or label.
    • Image If an image is part of the specified style, the image is displayed to the left of the cell’s text.

    Let's see all of them

    1. Single-click ViewController.m and to enable detail text label add/modify the following code

    2. Let's change table view cell styles again. Single-click ViewController.m and to enable detail text label add/modify the following code

    3. Let's change table view cell styles again. Single-click ViewController.m and to enable detail text label add/modify the following code

  5. Step 5: add an image
    For all style except UITableViewCellStyleValue2 we can add an icon.

    1. Prepare two images: one for selected row and second for not selected. In my case I have two dots (red and green) 24 pixels height and 24 pixels width.
    2. Drag both files to your project’s Images.xcassets.
    3. Single-click ViewController.m and to enable detail text label add/modify the following code

  6. Step 6: handling row selection
    Now we are going to implement the delegate method that is called after a row has been selected.

    1. Single-click ViewController.m and add/modify the following code

  7. Step 7: disable row selection
    The second table’s delegate method that allow us to handle row selection is tableView:willSelectRowAtIndexPath which is called before the row is selected. It can be used to prevent the row from being selected or even to change which row gets selected.

    1. Single-click ViewController.m and add/modify the following code
  8. Step 8: delete TableView row when swipe
    1. Single-click ViewController.m and add/modify the following code

3. Customizing Table View Cells with nib files

There are three basic approaches to create our own table view cells

  • one that involves loading a cell from a nib file,
  • a second that is similar, but loads the cell from a storyboard,
  • and a third that involves adding subviews to UITableViewCell programmatically when creating the cell.
  1. Step 1: create a new project
    Create a new project

    1. Select File | New | Project...
    2. Select Single View Application on the template selection sheet.
    3. As the product name enter iOS Table View Custom Cell Nib
    4. Set the Language to Objective-C, devices pop-up button to Universal and make sure the check box labeled Use Core Data is unchecked.
  2. >Step 2: create a new class
    1. In the Project Navigator, right-click the iOS Table View Custom Cell Nib group and select New File....
    2. Choose Cocoa Touch Class from the iOS Source section in the template dialog and then press Next
    3. Name the new class PeakInfoCell, make it a subclass of UITableViewCell.
    4. Make sure that the Also create XIB file check box is checked this time.
    5. Press Next and then press Create to save the files.
  3. Step 3: add a new code and a table view
    1. Single-click PeakInfoCell.h and add/modify the following code
    2. Single-click PeakInfoCell.m and add/modify the following code
    3. Single-click ViewController.h and add/modify the following code
    4. Single-click ViewController.m and add/modify the following code
    5. Select Main.storyboard to edit the storyboard.
    6. Find in the object library a Table View and drag it over to the View window.
    7. Add all necessary constraints to make sure that the table view is positioned and sized correctly.
    8. Now we have to link the table view to the outlet. Select the Main.storyboard file and in the Document Outline, Control-drag from the View Controller icon to the Table View icon. Release the mouse and select tableView in the pop-up.
    9. Set our controller class the data source for the table as we did it in part 2 step 2
      1. Select the table view in the Document Inspector and bring up the Connections Inspector (use shortcut: Alt + Command + 6).
      2. Drag from the circle next to dataSource in Outlets section to the View Controller icon in the Document Outline or above the view controller in the storyboard editor. This makes our controller class the data source for this table.
  4. Step 4: design our table view cell in Interface Builder
    Create a new project

    1. select PeakInfoCell.xib in the Project Navigator to open the file for editing.
    2. Look in the library for a Table View Cell and drag it to the GUI layout area. In my case this cell was already created, so there was no need for manual addition.
    3. In the the Attributes Inspector set the Identifier value to PeakInfoCellIdentifier.
    4. Select the table cell in the editing area to edit our table cell’s content view. Go to the library, drag out two Label controls, and place them in the content view where you want. Next set some font attributes. In my case left label (name label) was set to System Bold 20 and right label (height) to System Italic 17. Remember also to set correct constraints.
    5. Select the table view cell by clicking PeakInfoCellIdentifier in the Document Outline,

      bring up the Identity Inspector, and choose PeakInfoCell as the Class in Custom Class section.
    6. Switch to the Connections Inspector, where we will see the nameLabel and heightLabel outlets. Drag from the nameLabel outlet to the name label (left in my case) and from the heightLabel outlet to the height label (right).
  5. Step 5: run the application

4. Customizing Table View Cells with storyboard

We can also design table cells directly in the storyboard, which means that we don’t need to create an extra nib file. This is fine as long as we don’t want to share cell designs between different tables.

In this part we will do the same as in previous 3. Customizing Table View Cells with nib files part but without nib files.

  1. Step 1: create a new project
    Create a new project

    1. Select File | New | Project...
    2. Select Single View Application on the template selection sheet.
    3. As the product name enter iOS Table View Custom Cell Storyboard
    4. Set the Language to Objective-C, devices pop-up button to Universal and make sure the check box labeled Use Core Data is unchecked.
  2. Step 2: create a new class
    1. In the Project Navigator, right-click the iOS Table View Custom Cell Storyboard group and select New File....
    2. Choose Cocoa Touch Class from the iOS Source section in the template dialog and then press Next
    3. Name the new class PeakInfoCell, make it a subclass of UITableViewCell.
    4. Press Next and then press Create to save the files.
  3. Step 3: add a new code
    1. Single-click PeakInfoCell.h and add/modify the following code
    2. Single-click PeakInfoCell.m and add/modify the following code
    3. Single-click ViewController.h and add/modify the following code
    4. Single-click ViewController.m and add/modify the following code
  4. Step 4: add a table view and design our table view cell in the storyboard
    1. Select Main.storyboard to edit the storyboard.
    2. Find in the object library a Table View and drag it over to the View window.
    3. Add all necessary constraints to make sure that the table view is positioned and sized correctly.
    4. Find in the object library a Table View Cell and drag it over to the Table View window.
    5. In the Table View Cell section of Attributes inspector set
      1. Style as Custom,
      2. Identifier as PeakInfoCellIdentifier.
    6. Select the table cell in the editing area to edit our table cell’s content view. Go to the library, drag out two Label controls, and place them in the content view where you want. Next set some font attributes. In my case left label (name label) was set to System Bold 20 and right label (height) to System Italic 17. Remember also to set correct constraints.
    7. Select Main.storyboard, then the table view cell by clicking PeakInfoCellIdentifier in the Document Outline, bring up the Identity Inspector, and choose PeakInfoCell as the Class in Custom Class section.
    8. Switch to the Connections Inspector, where we will see the nameLabel and heightLabel outlets. Drag from the nameLabel outlet to the name label (left in my case) and from the heightLabel outlet to the height label (right).
    9. Now we have to link the table view to the outlet. Select the Main.storyboard file and in the Document Outline, Control-drag from the View Controller icon to the Table View icon. Release the mouse and select tableView in the pop-up.
    10. Set our controller class the data source for the table
      1. Select the table view in the Document Inspector and bring up the Connections Inspector (use shortcut: Alt + Command + 6).
      2. Drag from the circle next to dataSource in Outlets section to the View Controller icon in the Document Outline or above the view controller in the storyboard editor. This makes our controller class the data source for this table.
  5. Step 5: run the application