n.evermind
n.evermind

Reputation: 12004

How to change the class of UITableView programmatically?

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?

Upvotes: 2

Views: 547

Answers (2)

Cutetare
Cutetare

Reputation: 909

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. :)

Upvotes: 1

iEinstein
iEinstein

Reputation: 2100

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

Upvotes: 1

Related Questions