2016-12-19 3 views
1

TableSourceの新しいクラスを使用してXamarinでUITableViewを実装しました。私はMainViewからTableSourceを初期化していて、配列を渡しています。それから私は、MAINVIEWからその配列を更新しようとしていると私はエラーを取得しています:Xamarin - UITableViewは配列のデータをリロードします

Object reference not sent to an instance

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(); 
     }); 

    } 

に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; 
     } 
    } 

を私のアプローチです違う?どのように私はTableSourceのデータを更新するはずです。

+0

どのラインがエラーを投げていますか? –

+0

'' 'tableSource.updateArray(lVM.ordersArray);' ''これは1つです。これは、tableItems配列が割り当てられたものでなければなりません。 – BlackM

+0

あなたは正しいです。お詫びしますが、これはC#の2日目です!回答として投稿できますか? – BlackM

答えて

2

updateArraytableSource = new ListTableSource(this);より前に実行されており、このためtableSourceがnullです。 lVM.getInitialData(100);はおそらくupdateArrayをトリガーします。

関連する問題