UITableViewController will not call cellForRowAtIndexPath but numberOfSectionsInTableView and numberOfRowsInSection are set correctly

Decision:

The solution was to start by starting correctly with the UITableView and then adding UITableView delegates to the UIViewController as mentioned in the selected answer.

Foreword: I've read almost every article on the subject and nothing has helped.

I am embedding UITableViewController UITableView in UIViewController.

I understand that nothing will be called if the view is rendered, so I render it, and I can use NSLog to show that it got into those methods.

I tried to create a UITableViewController in the InterfaceBuilder and set my subclass as my own class that works! But that's not how I need to do it. Here's what I put together / did:

  • I am using InterfaceBuilder to control the UIViewController, but I am adding the UITableView programmatically
  • Both delegate and dataSource are set correctly and self.tableView is not nil
  • Despite correct logging, cellForRowAtIndexPath is never called and the displayed UITableView is empty.
  • I added delegates to my controller which didn't change anything

I have set the following in 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 tried to customize the view in many ways: by adding a UITableViewController directly, adding a UITableViewController to a UITableView (which seems to be correct), but nothing seems to work.

Perhaps there is a big step I forgot when working with InterfaceBuilder or some random thing that I forgot. Any help would be greatly appreciated. Thank!

** UPDATE ** This is how I add either UiTableViewController or 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];

      

+3


source to share


4 answers


Instead of parsing your code, I will show you a simple example of how to programmatically add a table view to a view controller that is not a UITableViewController

. Hope this helps you fix the problem 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 data source methods UITableViewDataSource

should happily run, including tableView:cellForRowAtIndexPath:

. At least they are here ...

I suggest you take this example and make it work in your environment. Then you gradually rework it step by step into the design you want. At some point everything will stop working, then you will have the source of your problem.

+6


source


The nested UIViewController is the controller of the UITableViewController container. There is a section in the UIViewController documentation:

https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926-CH3-SW81



I have a similar case where adding a call to the "addChildViewController" method fixes the problem:

[self addChildViewController:tvc];

      

+1


source


What is a frame for a tableView? You can print it with NSLog(@"%@", NSStringFromCGRect(self.tableView.frame));

. On suspicion, it is 0.0.0.0.

0


source


1 - check what numberOfRowsInSection is returning. Does the value return a value> 0? (sounds silly, I also made such mistakes)

2 - In your IB, check the table cell type - is it static or dynamic? If static it won't even look at your code.

3 - Are you calling reloaddata in the right place?

4 - In your view controller header file (or where it is declared), do you inherit from UITableViewDelegate and UITableViewDataSource delegates?

0


source







All Articles