Ios Xamarin表视图崩溃

Ios Xamarin表视图崩溃,ios,uitableview,xamarin,xamarin.ios,Ios,Uitableview,Xamarin,Xamarin.ios,我想在XAPP上使用UITableView 我尝试了UITableView示例,但它不起作用 当我使用this.Add(表)时导致崩溃。当我删除this.Add(table)时,它会显示空表 请帮帮我 这是我的密码 using Foundation; using System; using System.CodeDom.Compiler; using UIKit; namespace KUkyuko { partial class MyTableViewSource : UITable

我想在XAPP上使用
UITableView

我尝试了UITableView示例,但它不起作用

当我使用
this.Add(表)时导致崩溃。当我删除
this.Add(table)
时,它会显示空表

请帮帮我

这是我的密码

using Foundation;
using System;
using System.CodeDom.Compiler;
using UIKit;

namespace KUkyuko
{
    partial class MyTableViewSource : UITableView
    {
        public MyTableViewSource(IntPtr handle) : base (handle)
        {
            var table = this;
            string[] tableItems = new string[] {"Vegetables","Fruits","Flower Buds","Legumes","Bulbs","Tubers"};
            table.Source = new TableSource(tableItems);
            this.Add(table); //this code cause crash
        }
    }
    public class TableSource : UITableViewSource {

        string[] TableItems;
        string CellIdentifier = "TableCell";

        public TableSource (string[] items)
        {
            TableItems = items;
        }

        public override nint RowsInSection (UITableView tableview, nint section)
        {
            return TableItems.Length;
        }

        public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
        {
            UITableViewCell cell = tableView.DequeueReusableCell (CellIdentifier);
            string item = TableItems[indexPath.Row];

            //---- if there are no cells to reuse, create a new one
            if (cell == null)
            { cell = new UITableViewCell (UITableViewCellStyle.Default, CellIdentifier); }

            cell.TextLabel.Text = item;

            return cell;
        }
    }
}

我有点不确定你想要达到什么,但是

我会将tableview更改为tableViewController,如下所示:

partial class TableViewController : UITableViewController
{
    public TableViewController (IntPtr handle) : base (handle)
    {
        // Register the TableView's data source
        string[] tableItems = new string[] {"Vegetables","Fruits","Flower Buds","Legumes","Bulbs","Tubers"};
        this.TableView.Source = new TableSource(tableItems);
    }
}

public class TableSource : UITableViewSource {

    string[] TableItems;
    string CellIdentifier = "TableCell";

    public TableSource (string[] items)
    {
        TableItems = items;
    }

    public override nint RowsInSection (UITableView tableview, nint section)
    {
        return TableItems.Length;
    }

    public override nint NumberOfSections (UITableView tableView)
    {
        // TODO: return the actual number of sections
        return 1;
    }

    public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
    {
        UITableViewCell cell = tableView.DequeueReusableCell (CellIdentifier);
        string item = TableItems[indexPath.Row];

        //---- if there are no cells to reuse, create a new one
        if (cell == null)
        { cell = new UITableViewCell (UITableViewCellStyle.Default, CellIdentifier); }

        cell.TextLabel.Text = item;

        return cell;
    }
}
如果在viewController中使用表格,则需要在viewController中执行此部分,并为表格设置一个出口

将此代码添加到此中时,此代码崩溃的原因:

 var table = this;
 string[] tableItems = new string[] {"Vegetables","Fruits","Flower Buds","Legumes","Bulbs","Tubers"};
 table.Source = new TableSource(tableItems);
 this.Add(table); //this code cause crash as it is the same as this.Add(this)
希望这有帮助