从表格单元格导航到UIViewController

时间:2015-02-18 06:17:27

标签: xamarin.ios xamarin

我是xamarin ios的新手。我试图从表格单元格导航到uiviewcontroller.But在我的RowSelected方法中,这是在表源类中抛出" system.nullreference exception"我正在使用故事板,这是我的代码。

这是我的app起点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
    }
}

这是我的表视图控制器代码。

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

这是表源类代码。在这个类中,我在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代码。当我尝试从Listview.cs导航到_navigated_pa​​ge.cs&#34; nullreferenceexception&#34;有人说我的代码有什么问题

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

提前致谢。

1 个答案:

答案 0 :(得分:1)

您永远不会初始化_list,因此您始终在构造函数中传递空引用:

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

相反,这样做:

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