66

I have created a new Xcode project using Storyboards (tab view template). I added a couple of view controllers to my storyboard, and wanted to use a UITableView with static cells for one. I created it, but when I run in the simulator the cells don't appear. I haven't changed anything in the project except for this storyboard file. I am showing screenshots of the storyboard editor and the simulator running. The label shows up, so the view is loading correctly. I set the background color to gray so I can see the talbeview is loading. All cells are set to visible. I changed their style to Basic and edited the label, and added a disclosure indicator, that's all.

simulator xcode

adum
  • 2,890
  • 3
  • 25
  • 30

5 Answers5

207

Don't implement any of the methods below when you use the static table view:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
}
Kristian Glass
  • 37,325
  • 7
  • 45
  • 73
Joe Y
  • 2,109
  • 2
  • 12
  • 5
  • 30
    This means you have to delete implementations at all, not just insert empty methods :) – Nik Sep 18 '12 at 20:08
  • Yep, but I wonder how to get rid of the Xcode warning that now says the implementation is incomplete? – wcochran May 01 '13 at 22:56
  • @wcochran I would check if you're declaring those methods in your interface or if your class is not a subclass of UITableViewController. Both of those could lead to "Incomplete implementation" warnings in this case. – Justin Anderson May 10 '13 at 18:46
  • Thank you so much!! Being brand new to iOS development I've been following tutorials and seemingly none of them mention this...of course they mostly work with dynamic tables instead of static ones. – Jedidja Apr 01 '15 at 13:58
  • Useful when you have to implement an unwind segue on static cells. – quik_silv Sep 05 '15 at 01:52
  • So obvious, but so easy to overlook when you're using the Xcode template! Thanks Joe for saving me time! – KPM Oct 28 '19 at 09:20
102

As stated on Ray Wenderlich's website (in this post: Beginning Storyboards in iOS 5 Part 2, section "The Add Player Screen at Work" ):

One more thing about static cells, they only work in UITableViewController. The Storyboard Editor will let you add them to a Table View object inside a regular UIViewController, but this won’t work during runtime. The reason for this is that UITableViewController provides some extra magic to take care of the data source for the static cells. Xcode even prevents you from compiling such a project with the error message: “Illegal Configuration: Static table views are only valid when embedded in UITableViewController instances”.

Had the same issue but this makes things clear...

EeKay
  • 6,494
  • 3
  • 24
  • 24
  • 1
    So is there any way at all of using static cells in a UITableView which is inside a UIViewController using storyboards without embedding the UITableView in a UITableViewController in the storyboard? – Arunabh Das Dec 27 '12 at 20:28
  • from what i read in Ray's part is that this is being prevented because of a check and the illegal configuration alert. So although I am not sure i would bet my money on: No. – EeKay Jan 09 '13 at 11:28
  • @KarenAnne....don't use static cells. Just use a dynamic UITableView and make the container UIViewController a delegate and then use whatever static array you have to populate the UITableView. – Arunabh Das Dec 15 '13 at 03:37
  • 9
    XCode actually let me compile and run my app with a tableview inside a `UIViewController`. It was just a blank table. The Illegal Configuration error message would've helped – thefoyer Jan 22 '14 at 22:30
  • I was also able to compile with no error to be faced with a blank table. – LpLrich Feb 13 '14 at 14:06
35

Do you want to try using the TableViewController rather than the Generic View controller ?

Dennis Mathews
  • 6,897
  • 2
  • 26
  • 39
  • although i can't figure out how to get the table view to work if it's just a subview of a larger view, like in my screenshot. if i drag a Table View Controller over and let it be the whole screen, that works. but if i want to mix and match other UI elements, i have no idea. i can't figure out how to hook it up to a table view controller. – adum Dec 27 '11 at 05:13
  • 2
    You're only able mix and match only if you embed the table view controller inside a container view that's a child of your larger view. – Andrew Jul 09 '13 at 17:40
  • I had the same problem and it was because the darn generic UIViewController is defaulted to be the parent of the table view when dragging a UINavigationController from the menu. Why not load in a UITableViewController by default Apple? >_ – PostCodeism Dec 08 '14 at 20:00
12

You can add a Container View and embed a UITableViewController in that container. Then you can manage your static cells inside the new controller.

zqy
  • 131
  • 1
  • 4
  • 1
    Wow! That's why! I broke my brain, to figure out, why the former developer place UITableViewController in Container! – skywinder Feb 13 '14 at 08:25
1

I was experiencing the same problem, and the fix that worked for me was to present the static UITableViewController subclass using performSegue. Presenting the old way with [[self navigationController] present...] did not result in the static table view properly loading its cells.

dave
  • 1,150
  • 1
  • 13
  • 22