0
votes

I am new to xamarin ios. I am trying to navigate from table cell to present uiviewcontroller.But in my RowSelected method which is in table source class throws "system.nullreference exception" .I am using storyboard and this is my code.

This is my app starting point viewcontroller code

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

And this my table viewcontroller codes.

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 i am getting null reference exception in Rowselected method

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

And this is custom cells code.

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

}

This is the present uiviewcontroller code.When i trying to navigate from Listview.cs to _navigated_page.cs "nullreferenceexception" arised.Could anyone say what is problem with my code

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

Thanks in advance.

1

1 Answers

1
votes

You never initialize _list, so you are always passing a null reference in your constructor:

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

Instead, do this:

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