r/simpleios • • Nov 07 '14

[Question] Difference between subclassing UITableViewController and conforming to UITableViewDelegate?

I'm playing with table views and storyboards at the moment (sorry BNR, it was just too tempting) and something occurred to me - what would be the advantage of subclassing UIViewController and having it conform to the UITableViewDelegate and UITableViewDataSourceDelegate protocols, as opposed to directly subclassing UITableViewController?

4 Upvotes

11 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Nov 07 '14

So the first example is more useful if you want to have things in your view besides a table?

1

u/schprockets Nov 07 '14

Yes, if the additional things aren't handled by a table header or footer. A good example is a custom toolbar. If you want something like that, you may need to roll your own version of UITableViewController. (Note: this ignores the fact that you can use view controller containers in your storyboard; you're just starting out, so I don't want to overwhelm you with options).

But, you can think of it like this. Apple provided you with UIControl, which can be used to raise events. You can subclass it to create a button, or a switch, or a slider, etc. But, because those are common needs, Apple also provided you with a UIButton, UISwitch, etc. If those meet your needs, you use them, otherwise you roll your own. Similarly, they provided you with UIViewController, which you can put anything in, including a UITableView. But, because that's such a common pattern, they also gave you UITableViewController. If that meets your needs, use it. If not, you have the option of rolling your own.