I'm using Xamarin.iOS to build an iOS app. I'm using a storyboard and have created a UITableViewController
with a grouped table using custom cells. The cell contains a label to which I want to assign a value.
Overriding the getcell method in the datasource to set the text property of the label throws a null exception and I can't find why? I've checked the outlet and it's there. Any tips on how to find the error?
public partial class NameListScreen : UITableViewController
{
MyTableSource _source = null;
public override void ViewWillAppear (bool animated)
{
base.ViewWillAppear (animated);
LoadNameList();
}
void LoadNameList ()
{
var service = new MyService();
var names = service.GetAllNames();
_source = new MyTableSource(names);
this.TableView.Source = _source;
}
}
public class MyTableSource : UITableViewSource {
protected List<string> _names;
protected string _cellIdentifier = "MyCell";
public MyTableSource (List<string> items)
{
_names = items;
}
public override int RowsInSection (UITableView tableview, int section)
{
return _names.Count;
}
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
// Request a recycled cell to save memory
var cell = (MyTableCell) tableView.DequeueReusableCell(_cellIdentifier);
// If there are no cells to reuse, create a new one
if (cell == null)
cell = new MyTableCell();
cell.UpdateCell(indexPath, _names[indexPath.Item]);
return cell;
}
}
public partial class MyTableCell : UITableViewCell
{
public MyTableCell () : base ()
{ }
public MyTableCell (IntPtr handle) : base (handle)
{ }
public void UpdateCell(NSIndexPath indexPath, string name)
{
this._indexPath = indexPath;
this._id = track.TrackId;
this.NameLabel.Text = name; //This row throws exception as NameLabel is null, why?
}
}