26

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.

Constantin Jacob
  • 357
  • 1
  • 3
  • 12

5 Answers5

33

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?

Community
  • 1
  • 1
Can Poyrazoğlu
  • 33,241
  • 48
  • 191
  • 389
  • So I have to create cells as IBOutlets connect them and then create IBOutlets to connect my things inside the static cells? – Constantin Jacob Oct 01 '13 at 08:08
  • you are over-complicating it. you'll just design the cells in interface builder. and then link them to your code with regular ctrl-drag-and-drop style (just as you'd connect a button to code). and the only thing you need to make sure there is to make them `strong` references instead of the default `weak`. and you are done with the interface builder. the rest is just coding. – Can Poyrazoğlu Oct 01 '13 at 08:12
  • does this also work for multiple sections? what value would number of rows in sections then take? – brainray Jan 23 '14 at 14:57
  • i haven't tried it yet but it should definitely work. just edit the `-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section` method to return the number of rows in that section accordingly. it should return the number of rows that should be in that particular `section`. – Can Poyrazoğlu Jan 23 '14 at 22:04
  • you are welcome. I've also lost MANY hours trying to figure it out the first time :) – Can Poyrazoğlu Feb 19 '14 at 17:28
  • 1
    This great workaround may be broken in the new xcode 5.1 beta. I get an "illegal configuration" error: "Static table views are only valid when embedded in UITableViewController instances" – Ryan Mar 04 '14 at 07:18
  • Hmm, I don't have the beta in handy now so I can't test it, but are you sure that it won't work? The exact same error has been present in older versions of Xcode too, I doubt it has changed in 5.1DP. Have a look at these: http://stackoverflow.com/questions/9277473/want-to-create-a-cool-static-ui-but-static-table-views-are-only-valid http://iphonedevsdk.com/forum/iphone-sdk-development/111800-static-table-view-cells-only-work-in-a-uitableviewcontroller.html (there are many more). – Can Poyrazoğlu Mar 04 '14 at 09:02
  • 1
    It's so hilarious when the XCode allowed this to happen right? I meant We have to Set the Content Type from Dynamic Prototypes to Static Cells and do our Static Cells as we wish, but in the referring UIViewController most of all the UITableView methods work in order to let us manipulate the content of the UITableView! :) Thanks for the Nice Hack! It really worth get this thing done in this way! :) (Y) – Randika Vishman Mar 21 '14 at 11:48
  • 1
    Doesn't work for XCode 6, but knowledge for beginner – Henadzi Rabkin Oct 29 '14 at 15:07
  • @zest from Xcode 5.1, it started failing. but I'm keeping it for historical reference. maybe Apple will consider re-adding such features in the future. – Can Poyrazoğlu Oct 29 '14 at 17:27
  • 2
    There is a workaround but it is a little dirty. Drag a UIViewController to your Storyboard. The drag a CONTAINER VIEW inside the controller where you want your STATIC table to be. Delete the Segue and the View the container view created for you. Drag a UITableViewControler onto the scene, set it to STATIC cells, then CTRL+click from the CONTAINER VIEW to the UITableViewController and select EMBED. Tadaaaaa ! Dirty but nifty. – Benjamin Nov 26 '14 at 11:13
  • @CanPoyrazoğlu that's true! The project I used this method now appears to be failing! Let me explain a bit, I first did this in XCode (Version 5.1.1 (5B1008)) but installed in VMWare run Mac OS X Mavericks! I installed the same XCode V. in a MacBook Pro and it now fails to build. So I guess now I would have to re-do each of our tweaked Static Table Views in the formal manner. Please let me know your opinion! At least if you have a way to build this kind of a bit older project please let me know! It worth thanks a zillion! Thanks! – Randika Vishman Mar 31 '15 at 16:54
15

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.

Dannie P
  • 4,464
  • 3
  • 29
  • 48
4

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;
}
k06a
  • 17,755
  • 10
  • 70
  • 110
0

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.

etipton
  • 164
  • 1
  • 5
0

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.

Peter R.
  • 1
  • 2