16

How can I tell my UITableViewController to use my custom UITableView subclass for it's tableView instead of a regular UITableView?

HansUp
  • 95,961
  • 11
  • 77
  • 135
Michael Waterfall
  • 20,497
  • 27
  • 111
  • 168

3 Answers3

18

Set the UITableViewController.tableView property to an instance of your custom UITableView subclass.

Amber Star
  • 364
  • 2
  • 8
  • 2
    let's say I do this in `initWithStyle:` after I set `self = [super initWithStyle:style]`. How do I know what changes `-[UITableViewController initWithStyle:]` made to its `tableView`? I might be undoing those. See http://stackoverflow.com/questions/8512793/objective-c-how-to-change-the-class-of-an-object-at-runtime – ma11hew28 Dec 14 '11 at 22:45
  • 1
    Imho, the nicest place for this is to override `loadView` with something like `self.tableView = [[MyTableViewSubclass alloc] init]` or something of the sort. – CloakedEddy Nov 11 '16 at 09:09
  • If you do that, make sure to set the `delegate` and `dataSource` too or your table won't load anything. – Mark A. Donohoe Feb 25 '20 at 21:15
7

In interface builder associate the tableview class with your custom tableview in the identity inspector view

ennuikiller
  • 46,381
  • 14
  • 112
  • 137
-3

A UITableViewController is just a UIViewController with a UITableView instance and the viewController set as the delegate and dataSource for the table. The viewController implements stubs for those delegate methods.

You can do it yourself by having a UIViewController implement the UITableViewDataSource and UITableViewDelegate protocols. Then in the UIViewController's loadView (or viewDidLoad) method set the tableView instance's dataSource and delegate to self.

You can use your own table subclass there.

Ramin
  • 13,343
  • 3
  • 33
  • 35