views:

757

answers:

4

I'm having trouble creating a UIView (in Interface Builder) that contains a UITableView with some other object, such as a UIButton or UILabel. The UITableView always takes up the maximum amount of space in the UIView, regardless of the size of the UITableView object itself. The File Owner is a standard UITableViewController.

A: 

Unfortunately, creating a UITableView in Interface Builder (IB) is was very problematic for me. I ran into the same problems as you as a beginning developer and, after much frustration, just ended up abandoning IB for UITableViews.

The best solution for me was to just implement the UITableViewController (and the UINavigationController that you use as a header) programmatically. Once you figure out the whole Model-View-Controller paradigm, it is actually fairly straightforward.

Some good resources for dealing with them programmatically can be found in Apple's documentation with these names:

"Table View Programming Guide for iPhone OS"
"View Controller Programming Guide for iPhone OS"

Chris Redford
Look at Kendall's answer and my comment. Since 3.0, Apple cleaned up the Docs to better explain it. Using this approach you can still do Table Views in Interface Builder.
bpapa
Good to know, thanks.
Chris Redford
+4  A: 

Here's how to do this easily:

1) Create a table view controller with xib.

2) Change the inherited type in the .h file from UITableViewController, to UIViewController.

3) Add the protocols UITableViewDataSource, UITableViewDelegate to the .h file.

4) Open the xib, add in a view.

5) drag the table view in the xib under the view, resize as desired.

6) Wire the "view" property of the File's Owner to the View instead of the UITableView. The datasource and delegate properties of the table view should still be wired to File's Owner.

7) (optional) if you want to be able to reload or otherwise access the table outside of table view controller delegate methods that pass in a table view, make a UITableView * IBOutlet named "myTable" or the like, and wire the table in IB to that.


An alternate approach is to make a new UIViewController with xib, add a table to the xib, wire datasource/delegate to the file's owner, and make a new UITableViewController class which you use to copy the methods from into your view controller, then delete.

Kendall Helmstetter Gelner
This is the right way to do it. From Apple's docs: "If the view to be managed is a composite view in which a table view is one of multiple subviews, you must use a custom subclass of UIViewController to manage the table view (and other views). Do not use a UITableViewController object because this controller class sizes the table view to fill the screen between the navigation bar and the tab bar (if either are present)."
bpapa
Also one step I think you need to add Kendall - he will have to explicitly add the tableView as a property of his UIViewController class and make it an IBOutlet.
bpapa
Kendall/bpapa - Thanks for your help. Using your combined answers, I was able to address my issue.One thing about bpapa's last comment -- I did have to create a property called "tableView" in my UIViewController class; however, it seemed to work even if I didn't declare it as an IBOutlet or connected the IBOutlet to the actual tableView itself. I haven't quite figured out why it works... :-)
James Sun
Thanks bpapa, I added that step as well - I always do that too. James - glad you were able to get it working, but I would think the outlet needed to be connected!
Kendall Helmstetter Gelner
ALso, I had not seen that section of the Apple docs you brought up - very interesting.
Kendall Helmstetter Gelner
Yep, it wasn't that long ago that I had sworn off the idea of ever using IB with Table views. But the 3.0 docs really show the power - everything from doing the matter at hand to how to use IB effectively to create table cells, etc. Glad Apple demystified that stuff.
bpapa
A: 

Hello. Newbie here, stumbling thru and almost there... could someone elucidate what this means:

"Add the protocols UITableViewDataSource, UITableViewDelegate to the .h file."

I know what you mean in a forest sense, but the details of doing this in the code escape me. What does it mean to "add a protocol", codewise?

Thank you. This is exactly what I'm trying to do as well.

Brian Katz
Answered my own question with a little more research!<!--START CODE-->@interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate><!--END CODE-->... then have to implement certain methods in the MyViewController.m file, that would be taken care of by a UITableViewController if we were using that.Gracias.
Brian Katz
This isn't an answer, but a comment
Casebash
A: 

Cocoa with love has an article about Recreating UITableViewController to increase code reuse. This is useful if you can't use a UITableViewController, but want to make sure that your UIViewController will behave the same way.

Casebash