Daniel DirtyNative Martin Daniel DirtyNative Martin - 25 days ago 13
iOS Question

Xamarin iOS UITableView.Source NullReferenceException

I've created a UITableViewCell but when setting the source of the UITableView, I get a NullReferenceException.

public partial class SessionOverviewViewController : UIViewController
{

public List<BaseSessionController> Sessions;

public SessionOverviewViewController (IntPtr handle) : base (handle)
{
}

public override void ViewDidLoad()
{
base.ViewDidLoad();
tvTable.RegisterClassForCellReuse(typeof(SessionTableViewCell), "SessionCell");
SessionTableViewSource source = new SessionTableViewSource(Sessions);
tvTable.Source = source; //This line throws the Exception
}
}


The TableViewSource:

public class SessionTableViewSource : UITableViewSource
{
List<BaseSessionController> TableItems;
string CellIdentifier = "SessionCell";

public SessionTableViewSource(List<BaseSessionController> items)
{
TableItems = items;
}

public override nint RowsInSection(UITableView tableview, nint section)
{
return TableItems.Count;
}

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
SessionTableViewCell cell = (SessionTableViewCell)tableView.DequeueReusableCell(CellIdentifier);
BaseSessionController friend = TableItems[indexPath.Row];

//---- if there are no cells to reuse, create a new one
if (cell == null)
{
//cell = new SessionTableViewCell(new NSString(CellIdentifier), friend.FriendName, new UIImage(NSData.FromArray(friend.FriendPhoto)));
cell = new SessionTableViewCell(new NSString(CellIdentifier), friend);
}

//cell.UpdateCellData(friend.UserName, new UIImage(NSData.FromArray(friend.FriendPhoto)));

return cell;
}
}


And the Cell itself

public partial class SessionTableViewCell : UITableViewCell
{
public BaseSessionController Session;

public SessionTableViewCell (IntPtr handle) : base (handle)
{
}

public SessionTableViewCell(NSString cellId, BaseSessionController session) : base(UITableViewCellStyle.Default, cellId)
{
Session = session;
lblDate.Text = Session.Model.SessionStartTime.ToString("d");
}
}


Hopefully someone can see the mistake I made and can help me with this.

Thanks in advance

Answer Source

After reviewing the Storyboard, it was discovered that tvTable was actually a Scroll View. Frustratingly useless error message :)