简体   繁体   中英

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?

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

Hope this helps

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM