Forum Xamarin.iOS

Why I can't change change the UILabel.Text?

Lainio47Lainio47 USMember ✭✭
edited July 2016 in Xamarin.iOS

I decided to make a custom Cell for the UITableView.

Followed the Customizing a Table's Appearance and created a Cell like the tutorial.

Now in my Cell.cs I have the UpdateCell method like:

public void UpdateCell(string objecttext, string menutext,string rotatetext) { //defectImage = defectimage; ObjectText.Text = objecttext; MenuText.Text = menutext; RotateText.Text = rotatetext; }

ObjectText,MenuText and RotateText are the Labels in Cell.xib.

This is the Error I get: System.NullReferenceException: Object reference not set to an instance of an object
Am I doing it on a wrong way?

Answers

  • JohnHJohnH GBMember ✭✭✭✭✭

    A null reference means you are trying to access an object that doesn't exist. Put a breakpoint on the code, hover over ObjectText, and you'll see that it is null. You need to make sure that it has been set before you use it.

  • Lainio47Lainio47 USMember ✭✭

    Well I set the labels on Cell.xib but it seems that they aren't placed there?

  • JF.0444JF.0444 USMember ✭✭✭

    If your loading a cell from a xib you must Register it for cell reuse

    yourTableView.RegisterNibForCellReuse (YourCell.Nib, YourCell.Key);
    

    somewhere in view controller like ViewDidLoad or LoadView

    and your cs file for the xib should have something like

        public static readonly NSString Key = new NSString ("YourCell");
        public static readonly UINib Nib;
    
        static MerchantTableViewCell ()
        {
            Nib = UINib.FromName ("YourCell", NSBundle.MainBundle);
        }
    

    Or else all your UI elements will be null

Sign In or Register to comment.