2015-02-18 3 views
0

Я новичок в xamarin ios. Я пытаюсь перейти из ячейки таблицы, чтобы представить uiviewcontroller. Но в моем методе RowSelected, который находится в исходном классе таблицы, возникает «исключение system.nullreference». Я использую раскадровку, и это мой код.Переход к UIViewController из ячейки таблицы

Это мое приложение отправной точкой код 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 
    } 
} 

И это мои коды 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); 
     } 
    } 
} 

Это таблица класс источника code.In этот класс я получаю нулевое ссылочное исключение в методе 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); 
     } 
    } 
} 

Заранее спасибо.

ответ

1

Вы никогда не инициализировать _list, так что вы всегда пропускания пустой ссылки в конструкторе:

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

Вместо этого:

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

Привет джейсон спасибо за вашу помощь это работает, но это не отображает _navigated_page view controller. он отображает черный фон. – Balaji

+0

Попробуйте установить цвет bg на контроллере просмотра, чтобы убедиться, что он отображается. Это может быть просто проблема с этим VC, и вы не видите, что ожидаете. – Jason

+0

Привет, Джейсон, он отлично работает после изменения заднего фона. Спасибо за вашу помощь – Balaji

Смежные вопросы