I have a tableview inside a UIViewController. I also have some dynamic data, which I would like to load onto this tableview. I have attempted two approaches so far:
- I created a nested private class inside this UIViewController, which inherits from UITableViewSource. I then made a new instance of this class in ViewDidLoad and assigned it to my table view's Source property.
For this option, the override functions are called. However,I'm loading the lists dynamically in the ViewWillAppear, so the data source at this point is empty.
I tried creating a new instance of the nested class in the ViewWillAppear, but the override functions aren't called when I do this. They're only called if I do it in theViewDidLoad.
- The other option I tried was implementing the
IUITableViewDelegateand theIUITableViewDataSourcein my UIViewController class. Then in my ViewDidLoad assigned "this" to both the WeakDelegate and WeakDataSource properties of my table view.
The challenge I'm facing with this second option is : I keep getting this error:
[HomePageController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x185ce440
I've added this export attribute to the function, but it doesn't solve the issue:
[Export ("tableView:numberOfRowsInSection:")]
public nint RowsInSection (UITableView tableView, nint section)
{
return 1;
}
I prefer the first option, but why can't I initialize the class in the ViewWillAppear after loading my data? I need to call it here, since the ViewDidLoad is called only once.
This is what I've done:
public async override void ViewWillAppear (bool animated)
{
base.ViewWillAppear (animated);
try {
LoadingOverlay loadingOverlay = new LoadingOverlay (UIScreen.MainScreen.Bounds, "Loading...");
this.View.AddSubview (loadingOverlay);
await FetchAllData ();
loadingOverlay.Hide ();
if (tableViewNotice != null) {
if(allData != null)
{
if(allData.Count > 0)
{
homeDataSource = new HomeDataSource(this);
tableViewNotice.Source = homeDataSource;
}
}
}
} catch (Exception ex) {
Console.WriteLine (ex.Message + ex.StackTrace);
}
}