2

I have a UITableViewController and don't want to use a storyboard.

As far as I know, the UITableViewController takes care of initialising the UITableView, connecting it to its dataSource and delegate. This works very well in my case.

I would now like to change the class of the UITableView to a custom class (BVReorderTableView). This would be easily done in IB. However, once I do this programmatically, my UITableView is empty, that is it seems to be disconnected from its source and delegate.

Here is what I do in my init of the UIViewController:

-(id)init
{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        // Custom initialization
        self.tableView = [[BVReorderTableView alloc] initWithFrame:self.view.bounds  style:UITableViewStyleGrouped];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
}

What am I doing wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267
n.evermind
  • 11,944
  • 19
  • 78
  • 122
  • Did you check the datasource? Are the delegate methods firing (NSLog in them)? – Bourne Nov 19 '13 at 09:07
  • 1
    I would implement this in viewDidLoad, not on in init. – Cutetare Nov 19 '13 at 09:10
  • -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is not being fired @Bourne. But - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section is... how do I check the data source? I mean all is well if I don't use my custom BVReorderTableView class. Thanks a lot! – n.evermind Nov 19 '13 at 09:12
  • @Cutetare Thanks, that did the trick!! But why? Please put your answer as an answer so that I can mark it as correct. – n.evermind Nov 19 '13 at 09:14

2 Answers2

1

Please set your UITableView in viewDidLoad method not in initmethod. In init method view will initialize itself before subviews contained within it.

Hope this helps

iEinstein
  • 2,100
  • 1
  • 21
  • 32
1

You should implement this in viewDidLoad instead of init, because of the ViewController lifecycle. At viewDidLoad he already has all of the objects that are going to get used, which isn't necessarily true during init.

You can check this question for more information on ViewController life cycles. :)

Community
  • 1
  • 1
Cutetare
  • 913
  • 8
  • 24