【发布时间】:2019-09-05 22:41:57
【问题描述】:
我使用 TableSource 的新类在 Xamarin 上实现了 UITableView。我正在从 MainView 初始化 TableSource 并传递一个数组。然后我尝试从 MainView 更新该数组,但出现错误:
对象引用未发送到实例
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 中的数据。
【问题讨论】:
-
哪一行报错了?
-
tableSource.updateArray(lVM.ordersArray);这个。它应该与 tableItems 数组的分配有关。 -
您说得对,先生。道歉,但这是我在 C# 中的第二天!您可以将其发布为答案吗?
-
添加了我的评论作为答案
标签: ios uitableview xamarin xamarin.ios