I’m drawing a table view in MonoTouch by subclassing UITableViewController and UITableViewSource (not UITableViewDataSource!!!). In the constructor of my view controller, I set the table view source like that:
TableView.Source = new CustomTableViewSource();
And in my CustomTableViewSource, I’m constructing the cell of the table view. It’s work great except when I am simulating a memory warning in the iPhone simulator. If the view is hidden (e.g. an other view controller in the tab bar is selected), when I come back, the table view is just blank (white background, no table view anymore).
However, I’m doing nothing in the DidReceiveMemoryWarning callback. I also tried to set again the TableViewSource in the viewWillAppear (if received a memory warning) but no no more success…
Is it a garbage collector issue? I know that GC.Collect is called when receiving memory warning. Maybe the Garbage collector is just throwing away my view and I need to display it entirely again?
You should be initializing the table source in the ViewDidLoad method and not in the constructor. That method is called when a view controller is loaded and then every time the controller’s view needs to be re-created after a memory warning.
I’ve done some tests and I get similar results after a memory warning. However, I only get an empty table view. When I set the table source in the ViewWillAppear again, the table is populated with data normally.
The fact that your implementation in the ViewWillAppear does not work, makes me think that the method does not even get called. If that is the case, I am guessing that your table view is not being re-created when it needs to.
This can happen under certain circumstances when you are using controllers whose views are not created from a XIB file. In this case, you should create the view in the LoadView method (without calling base.LoadView()):
I had similar problems (table views where not re-created) with a complex hierarchy of tab controllers that contained navigation controllers, that contained view controllers etc. The problems were solved when I provided the LoadView method.
I’m out of guesses 😉