views:

76

answers:

2

I am running through the "Navigating with tables" section of Wrox' Professional iPhone Programming with MonoTouch by McClure et al, picking up the basics of putting together a hierarchical UI for iOS, and running into the following problem.

I have created a new "iPhone View with Controller" file (called ParametersViewController), deleted the UIView from it, added a UITableView, created an outlet for it (tableView) and connected the "File's Owner" view outlet to the UITableView, per the tutorial.

In the RowSelected method of this view's parent view, I instantiate my ParametersViewController, calling the default constructor, in which I want to set up the table view's data source:

this.tableView.Source = new DataSource(this, new [] {"one", "two", "three"});

(DataSource is a nested class which inherits from UITableViewSource)

All compiles and runs fine, up until the point where that line is executed. Turns out that this.tableView is null, so I get a NullReferenceException.

tableView is the outlet for the table. How can it be null? Can't I set up the table source here in the constructor? If not, where do I do it?

A: 

I'm not 100% clear on what you're doing where, but it might be that you need to access your UITableView in the UIViewController's ViewDidLoad method. At that point your NIB view(s) should be instantiated.

If that's not the problem, it may help if you could provide some more code so that we can see exactly what's going on. I think the lack of responses might due to people not being sure that they understand the nature of your problem exactly.

dommer
Just tried that, that's not the problem. There isn't really that much more code to provide - it's got to be something daft I've done in Interface Builder. I will update this question with the solution when I eventually find it.
tomfanning
Are you able to zip the project, upload it somewhere and post a link?
dommer
Thanks, but I solved this. Just got confused.
tomfanning
A: 

Solution to this was

  • Don't create an outlet called tableView
  • Make the view a subclass of UITableViewController, instead of UIViewController
  • Refer to the base.TableView property of my UITableViewController, rather than the nonsense outlet I created
tomfanning