【问题标题】:Xamarin - UITableView reload data for arrayXamarin - UITableView 重新加载数组的数据
【发布时间】: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


【解决方案1】:

我的感觉是updateArraytableSource = new ListTableSource(this); 之前执行,因此tableSource 为空。 lVM.getInitialData(100); 可能会触发updateArray

【讨论】:

    【解决方案2】:

    我的这个工人:

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

    在(1)列表tableItems中添加uiTextField的新值

    在(2)实际上是你的表源中的数据

    在 (3) 中直观地创建表格

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多