1

I am trying to set up a UITableView inside of a UIViewController. I'm doing this because it allows me to add a top bar with save and cancel buttons. I'm using storyboard and static cells to model the tableview to get input from the user (think of the create new event in Apple's calendar app). I have the view in Xcode, but when running it on my phone or the simulator, the tableview does not display. Here is the simple view in Xcode:

enter image description here

And this is how it displays when running it:

enter image description here

I've read about adding delegates and setting the datasource and such, but really this is all just going to be static cells with text fields, no data being loaded. Why is this happening and what can be done to fix it? Thanks!

idmean
  • 14,540
  • 9
  • 54
  • 83
Made2k
  • 222
  • 3
  • 15
  • 1
    It looks like you have `UITableViewDelegate` twice instead of `UITableViewDataSource`. I don't know if that's the issue, but you should fix it. By the way, if you embed your view in a `UINavigationController`, you can easily add bar items even to a `UITableViewController`. If you plan to use delegate and datasource methods, don't forget to set your delegate and datasource. – Marcus Adams Oct 10 '13 at 19:00
  • Good catch, thanks. That doesn't happen to be the problem, but with tweaking a lot of different things and changing values to attempt to debug this, I probably wouldn't have caught that. Though, fixing that and changing it to UITableViewDataSource does prompt me to add the cellForRowAtIndexPath and numberOfRowsInSection methods. – Made2k Oct 10 '13 at 19:07
  • Can you show a snap shot of your view hierarchy in storyboard? – Unheilig Oct 10 '13 at 19:25

4 Answers4

2

@Made2k It looks like you found a solution, but for others who come across this with the same issue, note that you can only use a UITableView with static cells inside of a UITableViewController. This is not a well-documented fact, but apparently only UITableViewController knows how to handle the layout of static cells, whereas a regular UIViewController just needs to implement the UITableViewDelegate and UITableViewDataSource protocols in order to handle display of content in a UITableView added either programmatically or via Storyboard/Nib.

Hopefully this will save someone some time. @Made2k's solution is fine, but does make your storyboard unnecessarily busy. If you don't need to use a UIViewController, then just do your work inside a regular UITableViewController and save yourself a headache.

ctlockey
  • 333
  • 3
  • 12
1

If you want to use a UITableView in a UIViewController you have to make the ViewController a data source and a delegate of the TableView and then implement methods

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

In case of a static table, in the cellForRowAtIndexPath you'd return outlets of the static cells. For a detailed description check out this answer: https://stackoverflow.com/a/19110821/3110536

Community
  • 1
  • 1
mhrvth
  • 87
  • 1
  • 1
  • 7
0

I ended up thinking about this in a different kind of way. The problem I thought I was having was I wanted all the features that a navigation controller provides, but I needed this to be the base of the controller, i.e. nothing to go back to. I was thinking that the only way to do this was to create a UIViewController and add the table view and such in there, but what I came up with is to simply just create a new navigation controller and now this view shows up as the root view like so:

enter image description here

I don't know if this is the best practice, but hopefully it can help somebody else if they are having this problem.

Made2k
  • 222
  • 3
  • 15
  • 1
    Did you get that idea from my comment? If not, I failed. :) – Marcus Adams Oct 10 '13 at 19:19
  • @MarcusAdams I think we were on the same thinking path but I didn't see your edit until I tried this. However, going back and changing some of the stuff from your comment did get me thinking about it, so yes, I got it from you haha. – Made2k Oct 11 '13 at 19:23
0

Man, following Hack really works! You should give it a try!
In my requirement I wanted to add buttons in my Static cells too!and Toggle the visibility of the TableView

[self.tableView setHidden:YES/NO];

and Reload it with new data

[self.tableView reloadData];

and so many things is possible with that way of doing it!

https://stackoverflow.com/a/19110821/1752988

Hope the above link would help you! (Y)

Community
  • 1
  • 1
Randika Vishman
  • 7,983
  • 3
  • 57
  • 80