Xamarin throws NullReferenceException when using c

2019-09-10 00:45发布

问题:

I am having a very similar problem to the one discussed at Xamarin custom UITableViewCell throws System NullReferenceException.

Nevertheless, after following the answer there, my issue has not been solved.

I have a custom UITableView BoardListCell. In my Storyboard, I have a UITableView. It is accessible in my ViewController via the outlet tableView.

In my view controller, I get the data. Then I do tableView.Source = new BoardsTableViewSource(sourceData);

My BoardsTableViewSource:

using System;
using System.Collections.Generic;
using Foundation;
using UIKit;

        public class BoardsTableViewSource : UITableViewSource
        {
            private List<List<Object>> data;
            Boolean normalBoards;

            public BoardsTableViewSource (List<List<Object>> data, bool normalBoards)
            {
                this.data = data;
                this.normalBoards = normalBoards;
            }

            public List<List<Object>> getData()
            {
                return data;
            }

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

            public override string TitleForHeader (UITableView tableView, nint section)
            {
                return "Header";
            }

            public override string TitleForFooter (UITableView tableView, nint section)
            {
                return "Footer";
            }

            public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
            {
                BoardListCell cell = (BoardListCell)tableView.DequeueReusableCell("BoardTableCell");

                List<Object> cellData = data [indexPath.Row];

                cell.setData("1","!"); //I have simplified the data for this example

            }
        }
    }

When debugging, I get the NullReference Exception on the cell.setData() line.

In StoryBoard, I have my BoardListCell's identifier set to BoardTableCell.

BoardListCell:

public partial class BoardListCell : UITableViewCell
    {
        public static readonly UINib Nib = UINib.FromName ("BoardListCell", NSBundle.MainBundle);
        public static readonly NSString Key = new NSString ("BoardListCell");

    public BoardListCell() : base()
    {
    }

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

    public static BoardListCell Create ()
    {
        return (BoardListCell)Nib.Instantiate (null, null) [0];
    }

    public void setData(String top, String bottom)
    {
        this.exp.Text = top;
        this.playTime.Text = bottom;
    }
}

exp and playTime are outlets to UILabel's. I have deleted and re-added the outlets, but this hasn't changed anything.

I don't know what the problem is. The only thing I can think of is because I am using a UITableView that I made with StoryBoard and am accessing it via an outlet. Nevertheless, I don't know what the problem is...

回答1:

DequeueReusableCell will return a null if it can't find a suitable cell to reuse. So you need to explicitly check for that before referencing your cell:

BoardListCell cell = (BoardListCell)tableView.DequeueReusableCell("BoardTableCell");

if (cell == null) {
  cell = new BoardListCell();
}