从 table 个单元格导航到 UIViewController

Navigating to UIViewController from table cell

我是 xamarin 的新手 ios。我正在尝试从 table 单元格导航到我的 RowSelected 方法中的 uiviewcontroller.But,该方法位于 table source class throws "system.nullreference exception" 中。我正在使用情节提要和这个是我的代码。

这是我的应用起点viewcontroller代码

namespace PopulatingTableView
{
    public partial class PopulatingTableViewViewController : UIViewController
    {

        // UITableView  table;

        public PopulatingTableViewViewController (IntPtr handle) : base (handle)
        {
        }

        public override void DidReceiveMemoryWarning ()
        {
            // Releases the view if it doesn't have a superview.
            base.DidReceiveMemoryWarning ();

            // Release any cached data, images, etc that aren't in use.
        }

        #region View lifecycle

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            button.TouchUpInside += (object sender, EventArgs e) => {
                this.NavigationController.PushViewController(new Listview(),true);
            };
        }

        public override void ViewWillAppear (bool animated)
        {
            base.ViewWillAppear (animated);
        }

        public override void ViewDidAppear (bool animated)
        {
            base.ViewDidAppear (animated);
        }

        public override void ViewWillDisappear (bool animated)
        {
            base.ViewWillDisappear (animated);
        }

        public override void ViewDidDisappear (bool animated)
        {
            base.ViewDidDisappear (animated);
        }

        #endregion
    }
}

这是我的 table viewcontroller 代码。

namespace PopulatingTableView
{
    public partial class Listview : UITableViewController
    {
        UISearchBar search;
        UITableView table;
        Listview _list;
        public Listview()
        {
        }
        public Listview (IntPtr handle) : base (handle)
        {
            search = new UISearchBar ();
            search.BackgroundColor = UIColor.Clear;

        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
            table = new UITableView(View.Bounds);
            customcell ();
            RectangleF search_frame = new RectangleF (0,80,300,30);
            search = new UISearchBar (search_frame);
            View.Add (search);
            View.Add(table);
        }

        public void customcell()
        {
            List<TableItems> tableItems = new List<TableItems>();

            tableItems.Add(new TableItems("Vegetables") {  ImageName = "date.png" });
            tableItems.Add(new TableItems("Fruits") {  ImageName = "date.png" });
            tableItems.Add(new TableItems("Flower Buds") { ImageName = "date.png" });
            tableItems.Add(new TableItems("Legumes") {  ImageName = "date.png" });
            tableItems.Add(new TableItems("Bulbs") {ImageName = "date.png" });
            tableItems.Add(new TableItems("Tubers") {  ImageName = "date.png" });
            table.Source = new TableSource(tableItems,_list);
        }
    }
}

This is table source class code.In this class 我在 Rowselected 方法中遇到空引用异常

namespace PopulatingTableView {

    public class TableSource : UITableViewSource {

        List<TableItems> tableItems;
        Listview parentcontroller ;

        NSString cellIdentifier = new NSString("TableCell");

        public event Action<int> OnRowSelect;

        public TableSource ()
        {

        }

        public TableSource (List<TableItems> items,Listview viewcontroller)
        {
            tableItems = items;
            parentcontroller = viewcontroller;
        }


        public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {   
//          new UIAlertView("Row Selected", tableItems[indexPath.Row].Heading, null, "OK", null).Show();
            parentcontroller.NavigationController.PushViewController (new _navigated_page(),true);
            tableView.DeselectRow (indexPath, true);
        }

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

        public override UITableViewCell GetCell (UITableView tableView,NSIndexPath indexPath)
        {

            CustomVegeCell cell = tableView.DequeueReusableCell (cellIdentifier) as CustomVegeCell;

            if (cell == null) {
                cell = new CustomVegeCell (cellIdentifier);
            }

            cell.UpdateCell (UIImage.FromFile ("Images/" + tableItems [indexPath.Row].ImageName)
//              , tableItems[indexPath.Row].SubHeading
                ,tableItems[indexPath.Row].Heading );

            return cell;
        }
    }
}

这是自定义单元格代码。

命名空间 PopulatingTableView {

public class CustomVegeCell: UITableViewCell  {

    UILabel headingLabel; //subheadingLabel;
    UIImageView imageView;
    UIButton talk,date;

    public CustomVegeCell (NSString cellId) : base (UITableViewCellStyle.Default, cellId)
    {

        SelectionStyle = UITableViewCellSelectionStyle.Gray;
        ContentView.BackgroundColor = UIColor.White;

        imageView = new UIImageView();

        talk = new UIButton () {
            Font = UIFont.FromName("Times New Roman", 10f),
            BackgroundColor = UIColor.Orange,
        };



        date = new UIButton () {
            Font = UIFont.FromName("Times New Roman", 10f),
            BackgroundColor = UIColor.Green,
        };



        headingLabel = new UILabel () {
            Font = UIFont.FromName("Times New Roman", 14f),
            TextColor = UIColor.Black,
            BackgroundColor = UIColor.Clear
        };


        ContentView.Add (headingLabel);
        ContentView.Add (imageView);
        ContentView.Add (talk);
        ContentView.Add (date);
    }

    public void UpdateCell ( UIImage image,string caption)
    {
        imageView.Image = image;
        headingLabel.Text = caption;

        talk.SetTitle ("Talk", UIControlState.Normal);
        talk.SetTitleColor (UIColor.White, UIControlState.Normal);

        date.SetTitle ("Date", UIControlState.Normal);
        date.SetTitleColor (UIColor.White, UIControlState.Normal);

    }

    public override void LayoutSubviews ()
    {
        base.LayoutSubviews ();

        imageView.Frame = new RectangleF(5, 5, 33, 25);
        headingLabel.Frame = new RectangleF(65, 5, 100, 25);
        talk.Frame = new RectangleF(200, 8, 50, 25);
        date.Frame = new RectangleF(260, 8, 50, 25);
    }
}

}

这是现在的 uiviewcontroller code.When 我试图从 Listview.cs 导航到 _navigated_page.cs "nullreferenceexception" arised.Could 谁说是什么我的代码有问题

namespace PopulatingTableView
{
    partial class _navigated_page : UIViewController
    {
        UILabel label1,label2,label3;

        public _navigated_page (IntPtr handle) : base (handle)
        {

        }

        public _navigated_page()
        {

        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad ();

            var frame = new RectangleF(10, 60, 300, 110);
            label1 = new UILabel(frame);
            label1.Text = "New Label";
            View.Add (label1);

            var frame1 = new RectangleF(10, 90, 300, 110);
            label2 = new UILabel(frame1);
            label2.Text = "New Label";
            View.Add (label2);

            var frame2 = new RectangleF(10, 120, 300, 110);
            label3 = new UILabel(frame2);
            label3.Text = "New Label";
            View.Add (label3);
        }
    }
}

提前致谢。

您从不初始化 _list,因此您总是在构造函数中传递空引用:

table.Source = new TableSource(tableItems,_list);

相反,这样做:

table.Source = new TableSource(tableItems, this);