How can I tell my UITableViewController
to use my custom UITableView
subclass for it's tableView
instead of a regular UITableView
?
Asked
Active
Viewed 1.2k times
16

HansUp
- 95,961
- 11
- 77
- 135

Michael Waterfall
- 20,497
- 27
- 111
- 168
3 Answers
18
Set the UITableViewController.tableView property to an instance of your custom UITableView subclass.

Amber Star
- 364
- 2
- 8
-
2let'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
-
1Imho, 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
-
2This is misleading. UITableViewController also includes keyboard avoidance, for example. – Luke Mar 08 '17 at 18:25
-
-
when you have a text field inside a cell the table is resized when the keyboard comes up and the text field is kept in view. – malhal Jul 02 '18 at 14:38
-
Not true, uitableviewcontroller doesnt have a view property. There are more differences – J. Doe Feb 09 '19 at 10:58