Overload loadView in subclass of UITableViewController

I'm trying to set up a UITableViewController so that its tableView belongs to my custom subclass. Now my loadView method looks like this:

- (void) loadView { MyTableViewSubclass* tv = [[[MyTableViewSubclass alloc]initWithFrame: CGRectZero style: UITableViewStylePlain]autorelease]; self.view = tv; self.tableView = tv; } 

Later I get alarms if I comment on the above method. So something is missing. But what?

Apple documentation says I should not call super in loadView. This makes sense because I want the view to have my class, not theirs.

Things I tried that don't help:

  • Rewrite the loadView method so that it creates a simple UITableView. This tells me that the source of the problem is not the implementation of my subclass.
  • Call [super viewDidLoad] from my loadView method. From Apple docs, it's not clear to me if this method is called from loadView or later. In any case, adding it to the end of my loadView method does not help.

One thing I tried that fixes the problem but defeats the goal:

  • Comment on my loadView method.

EDIT: Alarm shown below. This happens after the user enters any input. This also happens the same way if I create a simple UITableView instead of my subclass. A lot of things happen in the application, and something in my redefinition of loadView [or, rather, something is missing in my redefinition] leads to the fact that the state is different from the other, which in turn leads to failure. But I donโ€™t see a good way to track whatโ€™s different.

 2011-09-08 12:44:59.591 MyAppName[97649:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[MyTableViewSubclass scrollToRowAtIndexPath:atScrollPosition:animated:]: row (0) beyond bounds (0) for section (0).' 
+6
source share
2 answers

Turns out I need to set the dataSource and the delegate of my tableView as part of the load. So when I do this, everything works fine:

 - (void) loadView { MyTableViewSubclass* tv = [[[MyTableViewSubclass alloc]initWithFrame: CGRectZero style: UITableViewStylePlain]autorelease]; tv.dataSource = self; tv.delegate = self; self.view = tv; self.tableView = tv; } 
+12
source

William's answer will help me in the last hurdle.

To add a quick example, I usually use a template:

 class SomeTableViewController: UITableViewController { private var childView: SomeTableView! { return tableView as! SomeTableView } override func loadView() { tableView = SomeTableView(frame: UIScreen.mainScreen().bounds, style: .Plain) tableView.delegate = self tableView.dataSource = self view = tableView } } 

You can then freely access the custom view as a childView elsewhere in the ViewController.

+2
source

Source: https://habr.com/ru/post/896899/


All Articles