简体   繁体   中英

iOS Custom UITableViewController inside UIViewController

I'm pretty new on iOS developer and I come from Android developer, so, I'm trying to do this:

I want a storyboard that has a table view on the top and bellow some buttons and textfields. If I grab a TableViewController it fill up my table, but, I can't resize the table to put buttons/textfields bellow.

Untill now I've done this: A storyboard nested this way: ViewController -> View -> TableView -> CustomCell (does not work!)

I already have a Controller that can fill up my table view with its custom cell. However it only works when the storyboard is nested like this: TableViewController -> TableView (work but can't resize tableview )

My main storyboard (the only I have) has this class :

class SimpleController: UIViewController {

@IBOutlet weak var table =  UITableView();

override func viewDidLoad() {
    super.viewDidLoad()

   //The delegate works well when is called directly from a TableViewContoller
    //There's no error message but the table appears empty
    table?.delegate = UITCustomCell();
    table?.dataSource = UITCustomCell();
}

I'm also not sure if it is the best way to do this. Thank you so much!

I would create a parent view controller that has two view controllers as contained view controllers, which you can set up using the Container View element from the object library in interface builder:

在此处输入图片说明

Drag two containers into this parent and size them according, then using the "embed" segue type, link a table view controller and another view controller that has your buttons:

在此处输入图片说明

In your parent view controller class, prepareForSegue(_: sender:) will be called after viewDidLoad() once for each "embed" segue you've created, so you can get references to each of these view controllers like so:

class MyParentViewController: UIViewController {

    var buttonPanelViewController: MyButtonPanelViewController!
    var tableViewController: MyTableViewController!

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

        if let tableViewController = segue.destinationViewController as? MyTableViewController {
            self.tableViewController = tableViewController
        }
        else if let buttonPanelViewController = segue.destinationViewController as? MyButtonPanelViewController {
            self.buttonPanelViewController = buttonPanelViewController
        }
    }
}

class MyTableViewController: UITableViewController {}

class MyButtonPanelViewController: UIViewController {}

And lastly, you'll want to make sure you're setting Auto Layout constraints in interface builder so that this whole thing sizes correctly for the devices on which it will run. That's a whole 'nother topic that is out of scope for this question, but it's essential! This is great tutorial if you're not familiar: http://www.raywenderlich.com/50317/beginning-auto-layout-tutorial-in-ios-7-part-1

I thought the answer from Patrick Lynch was great. I have two more things to add that should be done for this to work fully (I'm still learning so maybe these things are too basic to even answer on this question).

After you do everything from his post: 1. Create new files and make one a ViewController and the other a TableViewController.
2. Attach each view in the storyboard to their new respective classes by selecting one of the new Scenes in the storyboard, then select Identity Selector from the Utilities screen, then pick the Class that matches the new file that you created. The button view to the new ViewController (called MyButtonPanelViewController) in the Lynch example and the TableViewController to MyButtonPanelViewController.

Click here to see what I'm talking about

My names are slightly different in the picture.

Once you have done the above, you can ctrl-click-drag the buttons, text fields, labels, etc. from the scene on to the appropriate ViewController to get access to the items in your views using IBOutlet, IBAction, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM