Can UIView be a UITableViewDelegate?


#1

If I understand the view hierarchy correctly, there can only be one view “controller” per screen, which makes absolute sense to me. But I’m a bit confused when it comes to the UITableView and UITableViewController. Say I have a “master” view for my iPad application which has multiple UIView areas within it. Each UIView is loaded from a separate class file which has extended UIView. So far, so good, and the views come up like I want them to.

Now one of the views I would like to have a table of web JSON information for the user to click on. In the iPhone world, I simply pushed another UITableViewController onto the screen, and it held the job of being the delegate and datasource for the self.tableView. However, in the iPad world, that same UITableViewController code, when added as-is inside of my UIView, doesn’t call any of the delegate functions, and I’m assuming this is because the screen already has a view controller on it.

So I’m struggling with understanding where the table delegation goes. Does this UITableViewController now have to become a standard UIView with a UITableView embedded? If so, can the UIView itself be a UITableViewDelegate and UITableViewDatasource? I’ve been trying this with no success. Perhaps the delegate and datasource have to be served by the master UIViewController that makes up my overall application window? I’ve been messing around with this for days now and can’t get my data to show up in the tableView at all.

Thanks for any assistance you can provide!


#2

A view object should rarely, if ever, be the delegate of another object. View objects (and model objects) are typically considered to be “dumb.” They know how to display something (views) or contain data (model objects), and no more. A controller object is where the logic of an application is implemented. Therefore, the data source and delegate of a table view in an application should be a controller object.

Then, you have to determine which controller object will handle the table view. This will be the view controller that manages the parent view of the table view. I imagine somewhere within a view controller’s loadView method, you create the instance of UITableView and add it to its view hierarchy. It is within that method you should set the data source and delegate to point at that view controller (self).


#3

Sometimes it just helps to talk through things in order to realize what dumb mistakes we’re making. It dawned on me after reading your response and thinking about it for several more hours that the whole entire screen has to be a UITableViewController, and that my other UIView portions of the screen could just be added to self.view. I now realize that self.tableView can be removed from the Superview, and put back wherever I want it. I now have my screen layout exactly as I wanted it. Thanks for the directional help!