Jorge
Jorge

Reputation: 1532

tableView initwithframe and storyboard

I'm trying to add a tableview as subview to my tableViewController, but I want to setup the cells in storyboard. It will be a static tableview.

This is the code for calling the tableview on button click.

- (IBAction)botaoAdicionar:(id)sender {
    AtividadesPraticadasTableView *tableview = [[AtividadesPraticadasTableView alloc] initWithFrame:CGRectMake(0, 170, 320, 320) style:UITableViewStylePlain];
    [self.view addSubview:tableview];
}

In the tableview class I have this:

@implementation AtividadesPraticadasTableView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

Now, I have a viewcontrollerin storyboard with a tableview, which the class of the tableviewI changed to this file AtividadesPraticadasTableView. It has three static custom cells in storyboard, therefore it opens a blank default tableview.

What am I missing?

Upvotes: 0

Views: 312

Answers (3)

jrturton
jrturton

Reputation: 119262

Static table views are entirely contained within the storyboard, and require information from the storyboard to display their content.

You've defined a static table view controller in the storyboard, populated it and set the tableView's custom class to your custom class, but when you want to add the table view you are just instantiating a table view of that class. That isn't going to get any of the information you've added to the storyboard.

In addition, the static cells information is read and understood by the UITableViewController, not the UITableView, so you are at the wrong level there too.

You need to do the following:

  • Get a reference to the storyboard, either from your original view controller's storyboard property (if it is on the same storyboard as your static table) or using storyboardWithName:bundle:.

  • instantiate the table view controller from that storyboard, using instantiateViewControllerWithIdentifier:. This will create a table view controller object containing all your static cells

  • Add this as a child view controller to your original view controller, using addChildViewController:
  • Add the table view controller's tableView as a subview

It may be simpler to add a container view in the storyboard to hold this view, and reveal it when the button is pressed, as Mike Slutsky suggested - this will do all of the instantiating and adding and child view controller-ing work for you, but the principle is still the same.

Also, adding a table view as a subview to a table view controller sounds very dodgy - a table view controller already has a table view as its view, and you can't really add subviews to that.

Upvotes: 2

Mike S
Mike S

Reputation: 11419

The thing your missing is the association between the programatically instantiated tableview and the UITableView that you put in your storyboard. You cannot just draw UITableViews in your storyboard and start instantiating new UITableViews in the controller's code and expect xcode to know which UITableView you wanted to use from the storyboard. Use an IBOutlet to connect a global variable for the controller to the UITableView in the storyboard, then the controller will know what you're trying to refer to. If you want that UITableView to appear on a button click, simply set the UITableView to hidden by default in the storyboard and then unhide it when the button is pressed.

Upvotes: 1

Related Questions