3
votes

I use Xamarin iOS designer to design custom TableViewCell class for my TableView. But all cell subviews properties (outlets) return null except cell itself.

My custom cell class:

partial class VehiclesTableCell : UITableViewCell
{
    public VehiclesTableCell(IntPtr handle) : base(handle) { }

    public void UpdateCell(string licensePlate) {
        licensePlateLabel.Text = licensePlate; //hit the null reference exception for licensePlateLabel 
    }
}

Generated partial class:

[Register ("VehiclesTableCell")]
partial class VehiclesTableCell
{
    [Outlet]
    [GeneratedCode ("iOS Designer", "1.0")]
    UILabel licensePlateLabel { get; set; }

    void ReleaseDesignerOutlets ()
    {
        if (licensePlateLabel != null) {
            licensePlateLabel.Dispose ();
            licensePlateLabel = null;
        }
    }
}

And GetCell of my Table Source class:

public class VehiclesTableSource : UITableViewSource
{
    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) {

        // Dequeueing the reuse cell
        var cell = (VehiclesTableCell)tableView.DequeueReusableCell(new NSString(typeof(VehiclesTableCell).Name));

        // Set cell properties
        cell.UpdateCell(LicensePlate);

        // Return the cell with populated data
        return cell;
    }
}

As we see genereted code has an outlet for licensePlate so why is its property null? Is storyboard not supposed to instantiate all of its subviews automatically? At least its happening in all other situations.

enter image description here

3
You should create cell if it is null in GetCell method. Check this in that they show how to create cell if it is null. - Janak Nirmal
Cell is not null, only its properties Labels, ImageViews etc - Arvis
Just a thought, check IBOutlets connections, as we are not using Xamarin studio can't tell much on that Check this too. - Janak Nirmal
@JanakNirmal, with the custom cell class registered for TableView in controller, the cell can be dequeued in the GetCell method of the UITableViewSource without the need for the extra null check. TableView.RegisterClassForCellReuse (typeof(MyCell), MyCellId); - Arvis
@Tomzie, yes i do. Just eliminate unnecessary registration of custom cell by removing line: TableView.RegisterClassForCellReuse(typeof(VehiclesTableCell), new NSString("VehicleCell")); in ViewDidLoad methode. - Arvis

3 Answers

8
votes

I was having the same problem with my custom TableViewCell. I found out the issue was with TableView.RegisterClassForCellReuse (typeof(MyCell), MyCellId). I had to change it to TableView.RegisterNibForCellReuse(UINib.FromName("MyCell", NSBundle.MainBundle), MyCellId) in order to get my .xib loaded.

2
votes

I came across this issue myself. This is what I did to resolve it:

  1. Instantiate the components of the subview in the constructor of the custom cell. In your case, something like:

    public VehiclesTableCell(IntPtr handle) : base(handle) { licensePlateLabel = new UILabel(); }

  2. Override the method LayoutSubviews:

    public override void LayoutSubviews () { base.LayoutSubviews (); licensePlateLabel.Frame = new RectangleF(63, 5, 33, 33); // Your layout here }

The info on this guide got me this far, but ideally there would be a way to accomplish this without instantiating and laying out the subviews components manually as they're already defined in the IOS designer. Hopefully someone can chime in with a better way to do this...

EDIT: I came across this answer which explained why I wasn't able to bind the custom table cell I created in the designer. TLDR: Ensure that Identity -> Class and Table View Cell -> Identifier are both set to the custom table view cell class name in the inspector window.

1
votes

I had this same issue, and as far as I have found out, if you are using storyboard you can just enter your custom class name in the properties of your table cell in the iOS designer and it should work. Calling RegisterClassForCellReuse() is what causes the null sub views issue. Once I removed that method call it seemed to work