Outlets in Xamarin custom TableViewCell are null

JustinGolden.5139JustinGolden.5139 USMember
edited February 2016 in Xamarin.iOS

I am trying to use a custom UITableViewCell. I am able to get the cell to instantiate, so that it is not null. However, all of the UILabel outlets in the cell are null. Thus, I am getting a null pointer exception.

I have made sure that my identifier in the Xib file of the cell matches what I am using.

Table View Cell in Xcode showing the Identifier

In my Source class:

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
        {
            tableView.RegisterClassForCellReuse(typeof(BoardListCell), new NSString(BoardListCell.Key));
            var cell = (BoardListCell) tableView.DequeueReusableCell(BoardListCell.Key);
            if (cell == null) 
            {
                var nib = UINib.FromName(BoardListCell.Key, NSBundle.MainBundle);
                cell = (BoardListCell)nib.Instantiate (null, null) [0];
            }

            cell.setData("top", "not top"); //Sample data
            return cell;
        }

In my BoardListCell class:

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 void setData(String top, String bottom)
        {
            exp.Text = top;
            playTime.Text = bottom;
        }
    }

When I debug, the cell is being created. However, its outlets are null. If I instantiate them in the constructor, I don't get the error, but this obviously defeats the purpose of making prototypes in xcode as it wipes out what I did with them in the Xcode interface builder.

What can I do? Many thanks!

Answers

  • trinnguyentrinnguyen VNMember ✭✭

    Just change the method GetCell:

    public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
    {
        tableView.RegisterClassForCellReuse(typeof(BoardListCell), new NSString(BoardListCell.Key));
        var cell = (BoardListCell) tableView.DequeueReusableCell(BoardListCell.Key, indexPath);
        /*if (cell == null) 
        {
            var nib = UINib.FromName(BoardListCell.Key, NSBundle.MainBundle);
            cell = (BoardListCell)nib.Instantiate (null, null) [0];
        }*/
    
        cell.setData("top", "not top"); //Sample data
        return cell;
    }
    

    The key is: add indexPath for this. So the tableView will auto init/reuse Cell based on Type that you've just registered. We should not care about nib.Instantiate

    var cell = (BoardListCell) tableView.DequeueReusableCell(BoardListCell.Key, indexPath);
    

    I use tableView.RegisterNibForCellReuse(BoardListCell.Nib, BoardListCell.Key) more than tableView.DequeueReusableCell but both of them should work well.

Sign In or Register to comment.