Reputation: 1095
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 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];
Upvotes: 3
Views: 8693
Reputation: 1279
The enclosing UIViewController is the container controller of the UITableViewController. The UIViewController documentation has a section on this:
I have a similar case, where adding a call to the method "addChildViewController" fixes the issue:
[self addChildViewController:tvc];
Upvotes: 1
Reputation: 13378
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:
@interface MyViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
}
@end
In the .m file:
- (void) loadView
{
CGRect mainViewFrame = CGRectMake(...);
self.view = [[[UIView alloc] initWithFrame:mainViewFrame] autorelease];
// You could make this even simpler if you set the table view as
// your main view
CGRect tableViewFrame = self.view.bounds;
UITableView* tableView = [[[UITableView alloc] initWithFrame:tableViewFrame style:UITableViewStylePlain] autorelease];
[self.view addSubview:tableView];
tableView.delegate = self;
tableView.dataSource = self;
}
That's pretty much it. Nothing spectacular, but with this simple setup the UITableViewDataSource
data source methods should be happily triggering, including tableView: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.
Upvotes: 6
Reputation: 6969
1 - check what numberOfRowsInSection returns. Does it returns value > 0? (sounds stupid I too made such mistakes)
2 - In your IB, check the table view cell type - is it static or dynamic? If static it won't even look at your code.
3 - Are you calling reloaddata at proper place?
4 - In your view controller header file (or wherever it is declared), are you inheriting from delegates UITableViewDelegate and UITableViewDataSource?
Upvotes: 0
Reputation: 1708
What is the frame for the tableView? You can print it with NSLog(@"%@", NSStringFromCGRect(self.tableView.frame));
. On a hunch, it's 0,0,0,0.
Upvotes: 0