UITableView with static cells does not appear
Asked Answered
P

5

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

Proctology answered 26/12, 2011 at 23:6 Comment(0)
K
35

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

Kassala answered 26/12, 2011 at 23:57 Comment(3)
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.Proctology
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.Annettannetta
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? >_<Natoshanatron
T
206

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
{
}
Tonetic answered 31/3, 2012 at 1:46 Comment(6)
This means you have to delete implementations at all, not just insert empty methods :)Flyte
Yep, but I wonder how to get rid of the Xcode warning that now says the implementation is incomplete?Botchy
@Botchy 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.Unremitting
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.Thermodynamic
Useful when you have to implement an unwind segue on static cells.Ventage
So obvious, but so easy to overlook when you're using the Xcode template! Thanks Joe for saving me time!Tarpeia
S
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...

Suksukarno answered 24/2, 2012 at 9:28 Comment(5)
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?Nuris
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.Suksukarno
@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.Nuris
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 helpedAarau
I was also able to compile with no error to be faced with a blank table.Jiggle
K
35

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

Kassala answered 26/12, 2011 at 23:57 Comment(3)
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.Proctology
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.Annettannetta
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? >_<Natoshanatron
M
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.

Moir answered 31/12, 2013 at 3:19 Comment(1)
Wow! That's why! I broke my brain, to figure out, why the former developer place UITableViewController in Container!Questor
D
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.

Durrett answered 6/10, 2014 at 19:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.