How to visually create and use static cells in a UITableView built into the UIViewController

I use Xcode 4.2 and create my user interface using storyboards. I need to create a view with contents above and below a UITableView, and I can do this using the UIViewController. UITableViewController does not allow adding content above or below the table. You can use the table header / footer, but this does not work for what I would like to achieve.

Now I have a UIViewController with a built-in UITableView. I can adjust the height and width of the UITableView accordingly, which gives me the layout of the user interface I'm looking for.

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

Illegal configuration: views of a static table are only valid when embedding in instances of UITableViewController

My question is: how do others get around this? Creating a table view with static cells and visualizing them is visually very enjoyable, but apparently this is not allowed for some reason that I cannot understand. I cannot switch to the UITableViewController due to my visual design requirements.

Any help would be greatly appreciated.

+53
ios objective-c
Jan 20 '12 at 17:53
source share
6 answers

You're right. In a storyboard, you cannot have a tableView with static cells embedded in the viewController. One way around this (I haven't tried it myself, so I'm not sure if this works) could be to create an instance of UITableViewController in a storyboard with static cells. Add the UIView instance to your viewController, and then programmatically load the tableView from the UITableViewController into the UIView of your viewController.

+5
Jan 20 '12 at 18:14
source share

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

In a storyboard, just create a UIViewController with a View Container inside the main view. Then connect the View Container to the UITableViewController that contains the static cells.

Similar:

enter image description here

You do not need a single line of code. Just control the click, drag and drop and select paste. The view controller console is being processed for you.

+133
Sep 24 '12 at 23:57
source share

pmd works, but if backward compatibility with iOS 5 is required, you can make an attachment programmatically 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]; } 

Remember to specify the storyboard ID for your UITableViewController with static cells.

+5
Oct 27
source share

I know this is an old question, but I have a patchwork solution to this problem.

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

  • Drag and drop some table elements into your interface (not in a UITableView) - add text and everything you need.
  • Make IBOutlet properties for your cells and synthesize them.
  • Drag the button and make it closed to the entire cell. Customize the Custom button to make it invisible - repeat for all cells
  • Add numeric tags to your buttons
  • Implement the following features. buttonDown is connected to the "Touch Down" event. buttonUp 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 you want in the buttonDown event and use the button to jump directly to the new view. This is very useful to me.

+3
Aug 12 '12 at 11:11
source share

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

-2
Jan 20 '12 at 18:14
source share

You can make it dynamic, and then toggle the scroll:

 [yourTableName setScrollEnabled:NO]; 
-four
Feb 23 '13 at 18:16
source share



All Articles