UITableView uses all available space in Interface

2019-03-16 05:53发布

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.

3条回答
Deceive 欺骗
2楼-- · 2019-03-16 06:02

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"

查看更多
Deceive 欺骗
3楼-- · 2019-03-16 06:04

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.

查看更多
对你真心纯属浪费
4楼-- · 2019-03-16 06:19

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.

查看更多
登录 后发表回答