TableViewCell Moving Objects for iOS Return Null

I use the Xamarin iOS constructor to create my own TableViewCell class for my TableView. But all the subviews properties of the cell (outlets) return null, except for the 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 Incomplete 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 source code 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 can see, the generated code has an output for licensePlate, so why is its property null? Shouldn't a storyboard automatically create all of its routines? At least this happens in all other situations.

enter image description here

+3
source share
3 answers

TableViewCell. , TableView.RegisterClassForCellReuse (typeof(MyCell), MyCellId). TableView.RegisterNibForCellReuse(UINib.FromName("MyCell", NSBundle.MainBundle), MyCellId), .xib.

+7

. , , :

  • . - :

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

  • LayoutSubviews:

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

, , , IOS. , - ...

EDIT: , , , . TL;DR: , Identity -> Class Table View Cell -> Identifier .

+2

, , , , iOS-, . RegisterClassForCellReuse() - , null. , ,

+1

Source: https://habr.com/ru/post/1628660/


All Articles