iOS: can not create custom view on UITableViewCont

2019-03-05 08:41发布

What am I doing?
I am learning iOS by building an app. My requirement shows some transactions on UITableViewController

I need to add header which I need to design with some specific information and style. I asked iOS: How do I make custom header section for Table? and realized that I need to make a custom UIView.

Problem is that I can't drag View on UITableViewController, XCode does not allow me to

enter image description here

Problem?
- If you see this shows not only the UIView but the entire iPhone screen
- I need to show this on my UITableViewController, with now what I have, how can I put the UIView as header on my UITableViewController?

2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-03-05 09:26

The view you've added the labels to is your view controller's main view. You will need to drag a View from your object library (bottom right menu) and add those labels in it. Then link the added view to your ViewController with an IBOutlet. After that you can set your tableView's header to be the added view:

self.tableView.tableHeaderView = headerView;

EDIT: If you want to do everything in code. Create the custom header view and drag it above your UITableView: https://stackoverflow.com/a/8053361/2604030

查看更多
干净又极端
3楼-- · 2019-03-05 09:29

Use a UIViewController rather than a UITableViewController

I recommend you don't use a UITableViewController as there's little advantage as far as I can see, whilst custom views like you're looking for are unnecessarily difficult.

Use a UIViewController and pull a UITableView object (Within the interface builder) on to that view. You can size and position it anywhere you like.

Remember to include the delegates within the header file, like below, and to connect the UITableView object delegate to the the viewController.

@interface ViewController: UIViewController<UITableViewDelegate, UITableViewDatasource>

Screenshot below of a UIViewController within the interface builder and I've added a UITableview object (which is greyed out here) and added a UIView at the top (coloured brown so you can see it clearly).

This of course is just an example, but hope it helps you get the idea.

enter image description here

Although in the above screen shot I'm using storyboard, though you can do this just the same with a Xib too.

I hope this helps.

查看更多
登录 后发表回答