Constantin Jacob
Constantin Jacob

Reputation: 357

Static table view inside UIViewController [Xcode 5]

I'm aware of the problem that one is not able to have static table view content in a UIViewController in

I don't get a warning/error but he also doesn't compile. Is there a trick to it or do I have to use the old ways around it?

Thanks in advance.

Upvotes: 26

Views: 27097

Answers (5)

Peter R.
Peter R.

Reputation: 1

As Dannie P mentioned above, using an IBOutletConnection is the way to go. To clarify on this a bit further:

Take the first cell from your static table view and ctrl+drag it into your UITableViewController. On the connection property window, select Outlet Collection on the Connection pull down menu.

Your should end up with code similar to this:

@property (strong, nonatomic) IBOutletCollection(UITableViewCell) NSArray *cells;

Next, ctrl+drag over all the rest of your cells (one at a time) onto the property you created above in the order you want them to appear in your static table view.

Upvotes: 0

Can Poyrazoğlu
Can Poyrazoğlu

Reputation: 34780

UPDATE: With the latest update (Xcode 5.1) it seems that it's no longer possible to put static cells inside regular UIViewController. My answer still applies for UITableViewController though.


Yes, you can have static table view content in UIViewController.

All you need to do is:

-Create the table's static cells in interface builder and design them the way you like.

-Make the UIViewController implement table view's data source and delegate:

@interface MyViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

-Connect the table view's delegate and dataSource to the view controller in interface builder

-Implement -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section to return the number of your cells. (e.g. return 10, yes simple as that)

-Connect your cells to your code as IBOutlets in Interface Builder. IMPORTANT: Make sure they are strong, weak won't work. e.g. @property (strong, nonatomic) IBOutlet UITableViewCell *myFirstCell;

-Implement -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath to return the correct cell at index path. e.g:

int num = indexPath.row;
UITableViewCell *cell;
switch (num) {
    case 0:
        cell = self.myFirstCell;
        break;
    case 1:
        cell = self.mySecondCell;
        break;
}
return cell;

If you apply all these steps, you should have working static cells that works for tables with not many cells. Perfect for tables that you have a few (probably no more than 10-20 would be enough) content. I've ran the same issue a few days ago and I confirm that it works. More info on my answer here: Best approach to add Static-TableView-Cells to a UIViewcontroller?

Upvotes: 33

k06a
k06a

Reputation: 18745

This is my try:

enter image description here

I have created container view and Table View Controller. Then I opened source code of Storyboard and changed destination identifier of container view to table view container identifier. Now make table view controller static...

UPDATE:

Just Ctrl+Drag from ContainerView to UITableViewController!

UPDATE 2:

Set embedded view controller class to smith like MYStaticTableViewController, witch should only have this method to provide -prepareForSegue calling to parent view controller:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([self.parentViewController respondsToSelector:@selector(prepareForSegue:sender:)])
        [self.parentViewController prepareForSegue:segue sender:sender];
}

UPDATE 3:

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    if ([self.parentViewController respondsToSelector:@selector(shouldPerformSegueWithIdentifier:sender:)])
        return [self.parentViewController shouldPerformSegueWithIdentifier:identifier sender:sender];
    return YES;
}

Upvotes: 4

etipton
etipton

Reputation: 164

Can's solution does break in XCode 5.1 :(

I found a workaround which builds off the same basic idea, but unfortunately requires a little more involvement: http://www.codebestowed.com/ios-static-tableview-in-uiviewcontroller/

To summarize, you can add TableViewCells directly to views (and create IBOutlets from them, etc), but in order for them to get "moved" to the TableView properly, you need to remove them from the view in code, and you also need to set Auto-Layout constraints in IB.

Upvotes: 0

Dannie P
Dannie P

Reputation: 4622

There's a way to improve Can's answer.

Connect your cells to code not as IBOutlet but as IBOutletCollection. If you name it as e.g. cells your code will look like this, which makes it slightly cleaner:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.cells.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.cells[indexPath.row];
}

The order in which you connect cells to outlet collection will be the order you see when run the app.

I can think of supporting several sections by linking their cells to several outlet collections.

Upvotes: 15

Related Questions