iOS Developer Library

Developer

UIKit Framework Reference UITableViewController Class Reference

Options
Deployment Target:

On This Page
Language:

UITableViewController

Import Statement


Swift

import UIKit

Objective-C

@import UIKit;

Availability


Available in iOS 2.0 and later

The UITableViewController class creates a controller object that manages a table view. It implements the following behavior:

  • If a nib file is specified via the initWithNibName:bundle: method (which is declared by the superclass UIViewController), UITableViewController loads the table view archived in the nib file. Otherwise, it creates an unconfigured UITableView object with the correct dimensions and autoresize mask. You can access this view through the tableView property.

  • If a nib file containing the table view is loaded, the data source and delegate become those objects defined in the nib file (if any). If no nib file is specified or if the nib file defines no data source or delegate, UITableViewController sets the data source and the delegate of the table view to self.

  • When the table view is about to appear the first time it’s loaded, the table-view controller reloads the table view’s data. It also clears its selection (with or without animation, depending on the request) every time the table view is displayed. The UITableViewController class implements this in the superclass method viewWillAppear:. You can disable this behavior by changing the value in the clearsSelectionOnViewWillAppear property.

  • When the table view has appeared, the controller flashes the table view’s scroll indicators. The UITableViewController class implements this in the superclass method viewDidAppear:.

  • It implements the superclass method setEditing:animated: so that if a user taps an Edit|Done button in the navigation bar, the controller toggles the edit mode of the table.

You create a custom subclass of UITableViewController for each table view that you want to manage. When you initialize the controller in initWithStyle:, you must specify the style of the table view (plain or grouped) that the controller is to manage. Because the initially created table view is without table dimensions (that is, number of sections and number of rows per section) or content, the table view’s data source and delegate—that is, the UITableViewController object itself—must provide the table dimensions, the cell content, and any desired configurations (as usual). You may override loadView or any other superclass method, but if you do be sure to invoke the superclass implementation of the method, usually as the first method call.

  • init(style:) - initWithStyle: Designated Initializer

    Initializes a table-view controller to manage a table view of a given style.

    Declaration

    Swift

    init(style style: UITableViewStyle)

    Objective-C

    - (instancetype)initWithStyle:(UITableViewStyle)style

    Parameters

    style

    A constant that specifies the style of table view that the controller object is to manage (UITableViewStylePlain or UITableViewStyleGrouped).

    Return Value

    An initialized UITableViewController object.

    Discussion

    If you use the standard init method to initialize a UITableViewController object, a table view in the plain style is created.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later

  • tableView tableView Property

    Returns the table view managed by the controller object.

    Declaration

    Swift

    var tableView: UITableView!

    Objective-C

    @property(nonatomic, retain) UITableView *tableView

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 2.0 and later

  • A Boolean value indicating if the controller clears the selection when the table appears.

    Declaration

    Swift

    var clearsSelectionOnViewWillAppear: Bool

    Objective-C

    @property(nonatomic) BOOL clearsSelectionOnViewWillAppear

    Discussion

    The default value of this property is YEStrue. When YEStrue, the table view controller clears the table’s current selection when it receives a viewWillAppear: message. Setting this property to NOfalse preserves the selection.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 3.2 and later

  • The refresh control used to update the table contents.

    Declaration

    Swift

    var refreshControl: UIRefreshControl?

    Objective-C

    @property(nonatomic, retain) UIRefreshControl *refreshControl

    Discussion

    The default value of this property is nil.

    Assigning a refresh control to this property adds the control to the view controller’s associated interface. You do not need to set the frame of the refresh control before associating it with the view controller. The view controller updates the control’s height and width and sets its position appropriately.

    The table view controller does not automatically update table’s contents in response to user interactions with the refresh control. When the user initiates a refresh operation, the control generates a UIControlEventValueChanged event. You must associate a target and action method with this event and use them to refresh your table’s contents.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 6.0 and later