How to visually create and use static cells in a UITableView embedded in a UIViewController
Asked Answered
S

6

53

I'm using XCode 4.2 and have built my UI using Storyboards. I need to create a view that has content above and below a UITableView and I can achieve this by using a UIViewController. A UITableViewController does not let you add content above or below the table. You can use the table header/footer but that doesn't work for what I would like to achieve.

I now have a UIViewController with a UITableView embedded in it. I can adjust the height and width of the UITableView accordingly which provides me the UI layout that I am looking for.

I can customize the static cells in the UITableView but when I try to build I get the following error:

Illegal Configuration: Static table views are only valid when embedded in UITableViewController instances

My question is how are others getting around this? Creating a tableview with static cells and laying them out visually is very nice but apparently that is not allowed for some reason that I cannot understand. I can't switch to a UITableViewController because of my visual layout requirements.

Any assistance would be greatly appreciated.

Shaunda answered 20/1, 2012 at 17:53 Comment(0)
J
5

You are right. In storyboard, you cannot have a tableView with static cells embedded in a viewController. One way around it (I have not tried it myself, though, so I am not sure if it works) can be that you create an instance of UITableViewController in storyboard with static cells. Add an instance of UIView to your viewController, and then programmatically load the tableView of the UITableViewController into the UIView of your viewController.

Jerrine answered 20/1, 2012 at 18:14 Comment(3)
Using NIBS is a possibility as I am reading further. Maybe this is the right approach to visually customize a cell then call it in the viewcontroller?Shaunda
My understanding from static cells is that, they can be only made within UITableViewControllers. The values (or titles) within them can be modified only when they are in the view (when you see them), with direct calling their index path. But, if your table is bing enough and you scroll away, and come back, vales change to whatever it was before. I think in your case, the Nibs are a good idea.Jerrine
It's not only possible, but quite easy, too, if you support iOS 6. Check my answer.Engagement
E
134

You can achieve this in Xcode 4.5 and later versions, assuming your app is targeted at iOS 6+.

In the Storyboard simply create a UIViewController with a View Container inside it's main view. Then hook up that View Container to a UITableViewController that contains static cells.

Just like this:

enter image description here

You don't need a single line of code. Just control click, drag and select embed. The view controller containment is handled for you.

Engagement answered 24/9, 2012 at 23:57 Comment(11)
If your view controller is subclassed from UITableViewController and you're still getting this warning, and you don't need/want a container, then see this post on how to workaround: plus.google.com/105709083169353686738/posts/VidCrsYPhgv (Requires a manual edit of the storyboard file.)Gosser
To add some extra help for beginners: (1) Place a Container View on your main view controller in Interface Builder. (2) Add a UITableViewController to your storyboard. (3) Control-click from the Container View and drag to the UITableViewController. (4) Select Embed.Incapacious
This should marked as the correct answer. Quick, easy, and even includes images.Incapacious
Can anyone confirm that this is still working with XCode 5? I am trying to CTRL-Click but my View won't connect to the TableViewController.Maples
Try holding right-click on the View Container and dragging to the Table View Controller before releasing.Engagement
For all people having the same difficulties. As said above use a "View Container" not a "View" (hope that helps s.o.) ;)Maples
@jeven, did you get an answer to your question? I am having the same issue. The steps pointed out in this answer do not work for me. I am using XCode 5.0.1Nuris
Are you using a Container View? You should be able to CTRL-click and drag an embed segue to the Table View controller. I just did it on a project on Xcode 5.Engagement
To the people having troubles with connecting it's because you're not using a "Container View". Note that it is not the same as a normal View.Animal
How would one access the controls within the tableVIew from the outside viewController?Bombsight
@ByronCoetsee I suggest using a delegate to communicate to the outer viewControllerEngagement
J
5

You are right. In storyboard, you cannot have a tableView with static cells embedded in a viewController. One way around it (I have not tried it myself, though, so I am not sure if it works) can be that you create an instance of UITableViewController in storyboard with static cells. Add an instance of UIView to your viewController, and then programmatically load the tableView of the UITableViewController into the UIView of your viewController.

Jerrine answered 20/1, 2012 at 18:14 Comment(3)
Using NIBS is a possibility as I am reading further. Maybe this is the right approach to visually customize a cell then call it in the viewcontroller?Shaunda
My understanding from static cells is that, they can be only made within UITableViewControllers. The values (or titles) within them can be modified only when they are in the view (when you see them), with direct calling their index path. But, if your table is bing enough and you scroll away, and come back, vales change to whatever it was before. I think in your case, the Nibs are a good idea.Jerrine
It's not only possible, but quite easy, too, if you support iOS 6. Check my answer.Engagement
B
5

pmd's answer works but in the event that backward compatibility with iOS 5 is required as well, you can do the embedding programatically using the View Containment API.

In the viewDidLoad method of your parent UIViewController:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
    MyTableViewController* vc =[storyboard instantiateViewControllerWithIdentifier:@"MyTableVC"];
    [self addChildViewController:vc];
    [self.view addSubview:vc.view];

    // ensure embedded view is aligned to top
    CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    vc.view.frame = frame;

    [vc didMoveToParentViewController:self]; 
}

Don't forget to specify a storyboard ID for your UITableViewController with the static cells.

Barbusse answered 27/10, 2012 at 7:47 Comment(0)
M
3

I know this is an old question but I have a scrappy solution to this issue.

I needed 3 static cells on a UIViewController so this is what I did:

  1. Drag in some Table View Cells into your interface (not into UITableView) - add text and whatever else you need.
  2. Make IBOutlet properties for your cells and synthesise them.
  3. Drag a button and make it cover the entire cell. Set the button to type 'Custom' so it appears invisible - repeat for all cells
  4. Add numeric tags to your buttons
  5. Implement the following functions. buttonDown is connected to the buttons 'Touch Down' event. buttonUp is connected to 'Touch Up Inside' AND 'Touch Up Outside'

    -(IBAction)buttonDown:(id)sender {
        if ([sender tag] ==  1) {
            myFirstCell.selected = YES;
        }
        else if ([sender tag] ==  2) {
            mySecondCell.selected = YES;
        }
        else if ([sender tag] ==  3) {
            myThirdCell.selected = YES;
        }
    }
    
    -(IBAction)buttonUp:(id)sender {
        myFirstCell.selected = NO;
        mySecondCell.selected = NO;
        myThirdCell.selected = NO;
    }
    

You can do whatever else you like in the buttonDown event and use the button to go directly to a new view. I find this pretty useful.

Mahogany answered 12/8, 2012 at 11:11 Comment(1)
I do like your "scrappy" approach very much, and I'm currently having a very hard time setting up my UIViewController(my needs very similar to the top question). May I ask, would your methodology allow for objects like UITextViews, textFields, etc.. That is, will drag-and-dropped IB objects have full functionality inside these static views. Because I am more or less in need of static UITableCellView's in a UIViewController, but I need full functionality for a UIImageView,UITextView,UITextField, and possibly some UIbuttons. @PatchBetsey
P
-2

I am not sure what you mean by static cells, but if you are trying to build the cells in IB, and then want to use it in your tableView, what you could do is in your cellForRowAtIndex you can call loadNibNamed passing the name of the .nib file you created for the cells as the parameter. Make sure that you have an outlet in your viewController which maps to the cell's .nib. Try exploring in these directions if that's what you are trying to achieve

Probative answered 20/1, 2012 at 18:14 Comment(1)
Table View → Attributes inspector → Content → [ Dynamic Prototypes | Static Cells ]. Also, when using Storyboards there's no longer a .nib file.Sothena
M
-4

You can make it dynamic and then switch of scrolling:

[yourTableName setScrollEnabled:NO];
Muriah answered 23/2, 2013 at 18:16 Comment(1)
He did not mean static in the sense of scroll not moving, but that the cells are predefined in the storyboard instead of provided by the data source and delegate.Subirrigate

© 2022 - 2024 — McMap. All rights reserved.