BlackM
BlackM

Reputation: 4065

Xamarin - UITableView reload data for array

I implemented UITableView on Xamarin using a new class for TableSource. I am initializing TableSource from MainView and pass an array. Then I am trying to update that array from MainView and I am getting error:

Object reference not sent to an instance

The code in MainVC:

public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        // Perform any additional setup after loading the view, typically from a nib.
        //Console.WriteLine("Event 1");
        lVM = new ListViewModel();
        lVM.dataUpdated += this.updateArray;
        lVM.dataUpdatedNetwork += this.networkUpdatedSQL;

        lVM.getInitialData(100);

        tableSource = new ListTableSource(this);
        tableV.Source = tableSource;

    }

    public void updateArray(object sender, EventArgs e) {

        InvokeOnMainThread(delegate
        {
            //Console.WriteLine("Update array event on VC for SQL count for array " + lVM.ordersArray.Count);
            tableSource.updateArray(lVM.ordersArray);
            tableV.ReloadData();
        });

    }

The code in TableSource:

public class ListTableSource : UITableViewSource
    {
        ListViewController listVCRef;
         ArrayList tableItems;
         string cellIdentifier = "TableCell";

        public ListTableSource(ListViewController listVC)
        {
            tableItems = new ArrayList();
            listVCRef = listVC;
        }

        public void updateArray(ArrayList items) { 
            tableItems = new ArrayList(items);
            Console.WriteLine("Items " + tableItems.Count);
        }
        public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
        {
            new UIAlertView("Alert", "You touched: " + tableItems[indexPath.Row], null, "OK", null).Show();
            tableView.DeselectRow(indexPath, true);
        }

        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
        {
            UITableViewCell cell = tableView.DequeueReusableCell(cellIdentifier);

            if (cell == null)
            {
                cell = new UITableViewCell(UITableViewCellStyle.Subtitle, cellIdentifier);
            }

            Order currentOrder = tableItems[indexPath.Row] as Order;

            cell.TextLabel.Text = currentOrder.comment;
            cell.DetailTextLabel.Text = currentOrder.orderDate.ToString();
            return cell;

        }

        public override nint RowsInSection(UITableView tableview, nint section)
        {
            return tableItems.Count;
        }

        public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
        {
            return 80;
        }
    }

Is my approach wrong? How I am supposed to update the data in TableSource.

Upvotes: 2

Views: 602

Answers (2)

user7396942
user7396942

Reputation:

This worker for my:

button.TouchUpInside += (s, e) => {
                tableItems.Add(textfield1.Text); //(1)
                table = new UITableView(View.Bounds);  
                table.Source = new TableView(tableItems.ToArray()); //(2)
                table.Frame = new CoreGraphics.CGRect(0, 350, this.View.Frame.Width, 300); //(3)
                Add(table);
            };

in (1) the list tableItems add new value of uiTextField

in (2) Actually the datas in Source of your table

in (3) create visually the table

Upvotes: 0

Claudio Redi
Claudio Redi

Reputation: 68400

My feeling is that updateArray is executed before tableSource = new ListTableSource(this); and because of this tableSource is null. lVM.getInitialData(100); is likely triggering updateArray.

Upvotes: 2

Related Questions