Solution:
The solution was to start by properly starting with a UITableView and then adding the UITableView delegates to the UIViewController as outlined in the selected answer.
Preface: I have read nearly every article on the matter and nothing advised has helped.
I’m embedding a UITableViewController’s UITableView into a UIViewController.
I understand that nothing will be called unless the view is rendered, so I render it and I can use NSLog to show that it hit those methods.
I tried making a UITableViewController in InterfaceBuilder and setting my subclass as its custom class which worked!! But that’s not how I need to go about it. Here’s what I’ve gathered / done:
- I am using InterfaceBuilder to manage the UIViewController but I’m adding the UITableView programmatically
- Both the delegate and dataSource are set properly and self.tableView is not nil
- Despite the valid logging, cellForRowAtIndexPath is never called and the rendered UITableView is blank
- I added the delegates to my controller which did not change anything
I have set the following on the UITableViewController:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSLog(@"%@", self.questions); // This outputs the questions array as not empty and works
// As you can see I am also returning 1.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"%d", [self.questions count]); // This outputs 4 as it should
return [self.questions count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// All we need to focus on is this NSLog which never outputs
NSLog(@"Was called and array is, %@" self.questions);
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
return cell;
}
I’ve tried setting the view many ways: Adding UITableViewController directly, adding the UITableViewController’s UITableView (which seems to be the correct way), but nothing is working.
Perhaps there is a major step I have forgotten when when working with InterfaceBuilder or some random thing I have forgotten. Any help would be appreciated. Thanks!
** UPDATE **
Here is how I add either the UiTableViewController or the UITavleView
GTTableViewController *tvc = [[GTTableViewController alloc] initWithStyle:UITableViewStylePlain];
// Either
[self.view addSubview:tvc.view];
// OR
[self.view addSubview:tvc.tableView];
// Just to make sure everything is still ok.. and I see the 2/3 TV methods fire.
[self.tableView reloadData];
Instead of trying to analyze your code, I will show you a simple example how to programmatically add a table view to a view controller that is not a
UITableViewController. Hopefully this will help you getting your problem ironed out by yourself.In the .h file:
In the .m file:
That’s pretty much it. Nothing spectacular, but with this simple setup the
UITableViewDataSourcedata source methods should be happily triggering, includingtableView:cellForRowAtIndexPath:. At least they do here…I suggest you take this example and make it work in your environment. Then you slowly rework it step-by-step into the design you want. At some point things will stop working, then you will have the source of your problem.