UITableView uses all available space in Interface Builder

∥☆過路亽.° 提交于 2019-12-20 14:09:57

问题


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.


回答1:


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.




回答2:


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"




回答3:


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.



来源:https://stackoverflow.com/questions/1615895/uitableview-uses-all-available-space-in-interface-builder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!