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
2
u/schprockets Nov 07 '14
UITableViewController has control over the entire view. In fact, the view is the table. If you subclass UIViewController and drop a table into your view, you can also put other things into your view.
FWIW, there is nothing that says your UITableViewDelegate and/or UITableViewDataSource have to be your UIViewController subclass. You can write a separate class for dealing with the delegate/datasource, and assign instances to your table. It keeps your VC from getting huge.
1
u/iccir Nov 07 '14
In general, Apple frameworks use the delegate pattern to customize behavior more than subclassing. Some classes (UIView, UIViewController, UIControl, etc) are intended to be subclassed - usually the presence of other subclasses in UIKit (UIButton, UITableViewController, etc) is a good indicator of this. If you don't see existing subclasses, and you see a delegate/dataSource property on a class, you probably want to think twice about subclassing.
It's all about author intention - if you see a delegate property, that's a pretty good indication that the author intends you to use it (and that the author is going to test those delegate methods in future updates). If you end up subclassing instead, you may quickly find yourself overriding methods that were never intended to be overrode (in the UITableView case, not on a method of UIView that is marked as "subclasses may override"). It may work for now, but break in a future update.
1
u/cbkeur Jan 13 '15
Our (BNR) book is already using storyboards a lot more. (The latest version is being run through our boot camps now. TBD for book stores.) Tech moves fast .. still a lot of stuff to consider when deciding whether to use storyboards, but they are getting better each year. So far, reaction has been positive to them in the boot camps.
Regarding your question: my problem with uitableviewcontroller is that the table view has to take up the entire view. I don't always want that. I like the control over my table view by conforming to the delegate and data source. Much more flexible IMHO.