Search code examples
objective-cxcode4uitableview

Associating a UITableView with a TableViewController


Can anyone describe how it is possible to have a TableViewController with its xib file having a at its root and the uitableview as a subview? I believe the TVController somehow assumes that UITableView will fill the entire area. Why is that? I have a really specific need to build a kind of side and bottom tabbed interface with a UITableView as its main area. Pressing tabs changes the predicate for the fetchedresultscontroller etc etc. The reason I want this is because of the breadth and depth of the data categories in the data model. I rally need to flatten the menu structure a lot...other wise with a table and navbar controller structure, user experience will be akin to sinking to ever deeper depths of a cavern! My idea is tried and true in other paradigms...in iOS it almost looks like it's Apple's way or the highway. I am OK with APPLE of course no gripe. But I really would like to create this. I tried adding a new tableviewcontroller complete with xib and then removing the UITableView in IB and replacing with a UIView and a UITableView as a subview, hooking up (I believe) the delegate to the file's owner. I created an IV tableView for when I want to reference it and again used IB to hook it up in IB

Try to run it and it whines that... [UITableViewController loadView] loaded the "TabbedTableController" nib but didn't get a UITableView.' Really can't seem to get my head around what the issue is here. There doesn't appear to be anymore I can do to hook the UITableView up!

Any help would be terrific. I'll send you a Christmas card in desperation :^) Also...why should it be so and how strict is this UITableView fullscreen thing? Enlighten me if you can. The docs I have read don't want to.

Regards Keith


Solution

  • A UITableViewController does assume that the root view (i.e. the controller's view property) will be a UITableView, thus the table view fills the screen. When you need a view that combines UITableView with other top level views, you will need to do a little more work but it's not hard:

    Your view controller will not subclass UITableView. Instead, do this:

    @interface MyViewController : UIViewController 
      <UITableViewDelegate, UITableViewDataSource>
    @property (nonatomic, weak) IBOutlet UITableView* tableView;
    

    In Interface Builder, drop in a UITableView and whatever other controls you need. The table view can be any size and in any location in the view hierarchy. Also in Interface Builder, ctrl-drag from the table view to your VC and set the delegate and dataSource outlets, and ctrl-drag from your VC to the table view to set the tableView outlet.

    Your view controller implementation should be the typical table view controller implementation: cellForRowAtIndexPath, etc.

    A UITableViewController is more or less just all of the above work packaged up into a single unit for you.