Label in user cell is NULL when populating UITableViewController

I am using Xamarin.iOS to create an iOS application. I use a storyboard and created a UITableViewControllergrouped table using custom cells. The cell contains the label to which I want to assign a value.

Overriding the getcell method in the data source to set the text property of the label causes a null exception, and I cannot find why? I checked the outlet and there. Any tips on how to find the error?

public partial class NameListScreen : UITableViewController
{
    MyTableSource _source = null;

    public override void ViewWillAppear (bool animated)
    {
        base.ViewWillAppear (animated);

        LoadNameList();          
    }

    void LoadNameList ()
    {
        var service = new MyService();
        var names = service.GetAllNames();

        _source = new MyTableSource(names);

        this.TableView.Source = _source;
    }
}

public class MyTableSource : UITableViewSource {

    protected List<string> _names;
    protected string _cellIdentifier = "MyCell";

    public MyTableSource (List<string> items)
    {
        _names = items;
    }

    public override int RowsInSection (UITableView tableview, int section)
    { 
        return _names.Count;
    }

    public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
    {
        // Request a recycled cell to save memory
        var cell = (MyTableCell) tableView.DequeueReusableCell(_cellIdentifier);

        // If there are no cells to reuse, create a new one
        if (cell == null)
        cell = new MyTableCell();

        cell.UpdateCell(indexPath, _names[indexPath.Item]);

        return cell;
    }
}


public partial class MyTableCell : UITableViewCell
{

    public MyTableCell () : base ()
    { }

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

    public void UpdateCell(NSIndexPath indexPath, string name)
    {
        this._indexPath = indexPath;
        this._id = track.TrackId;
        this.NameLabel.Text = name;   //This row throws exception as NameLabel is null, why? 
    }
}
0
source share
1 answer

, , xib . .

// Inside MyTableCell
public static MyTableCell Create()
{
    NSArray topLevelObjects = NSBundle.MainBundle.LoadNib("MyTableCell", null, null);
    MyTableCell cell = Runtime.GetNSObject(topLevelObjects.ValueAt(0)) as MyTableCell;
    return cell;
}

Xamarin. , UINib ( , Xamarin Studio ).

GetCell

public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
    // Request a recycled cell to save memory
    var cell = (MyTableCell) tableView.DequeueReusableCell(_cellIdentifier);

    // If there are no cells to reuse, create a new one
    if (cell == null)
        cell = MyTableCell.Create();

    cell.UpdateCell(indexPath, _names[indexPath.Item]);
    return cell;
}

[Register("MyTableCell")] .cs XIB, . MyTableCell XIB .

Xamarin ( , iOS), .

0

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


All Articles