2013-06-10 2 views
0

Я пытаюсь добавить аннотацию с моим табличным представлением. Добавлена ​​начальная аннотация, но всякий раз, когда я нажимаю на мое табличное представление, вызываете другое место. Это дает мне ошибку (nullexception). Есть ли кто-нибудь, кто может мне помочь?Добавление аннотации MapView - Xamarin

using System; 
using System.Drawing; 
using MonoTouch.Foundation; 
using MonoTouch.UIKit; 
using MonoTouch.MapKit; 
using MonoTouch.CoreLocation; 

namespace FieldMobileApp 
{ 
    public partial class MainRightViewController : UIViewController 
    { 
     UITableView table; 
     MKMapView mapView; 
     BasicMapAnnotation annotation = new BasicMapAnnotation (new CLLocationCoordinate2D (48.857, 2.351), "Paris", "City of Light"); 
     public MainRightViewController() : base ("MainRightViewController", null) 
     { 
      this.Title ="Right View Controller"; 
     } 

     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. 
     } 
     string[] tableItems = new string[] { "Harris", "Lubbock", "Galveston", "College Station", "Sugar Land", "Tulsa","Meyerland","Pearland","Linkwood","Valley Hills","Simav","Bursa"}; 
     double[] lat = new double[] { 42, 36, 25, 18, 19, 58, 62, 10, 5, 6, 44, 55, 62 }; 
     double [] lng= new double[] { -42, -36, -25, -18, -19, -58, -62, -10, -5, -6, -44, -55, -62 }; 

     public override void ViewDidLoad() 
     { 
      base.ViewDidLoad(); 
      table = new UITableView (new RectangleF(80,80,250,500)); 
      table.AutoresizingMask = UIViewAutoresizing.All; 
      table.Source = new TableSource (tableItems); 
      Add(table); 

      mapView = new MKMapView(new RectangleF (538,78,408,639)); 
      mapView.AutoresizingMask = UIViewAutoresizing.FlexibleDimensions; 
      View.AddSubview(mapView); 

      // create our location and zoom for los angeles 
      var coords = new CLLocationCoordinate2D(48.857, 2.351); 

      // add a basic annotation 
      //var annotation = new BasicMapAnnotation (new CLLocationCoordinate2D (48.857, 2.351), "Paris", "City of Light"); 
      mapView.AddAnnotation (annotation); 

      // Perform any additional setup after loading the view, typically from a nib. 
     } 




     public void ciz(int i) 
     { 

      var annotation = new BasicMapAnnotation (new CLLocationCoordinate2D (lat[i], lng[i]), "Tayfun Place", "City of Knowledge"); 

      Console.WriteLine ("{0},{1}", lat [i], lng [i]); 
        // following line gives me an error, null exception! 
      mapView.AddAnnotation (annotation); 

     } 


     public class TableSource : UITableViewSource { 
      protected string[] tableItems; 
      protected string cellIdentifier = "TableCell"; 
      public MainRightViewController parentController= new MainRightViewController(); 

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

      /// <summary> 
      /// Called by the TableView to determine how many cells to create for that particular section. 
      /// </summary> 
      public override int RowsInSection (UITableView tableview, int section) 
      { 
       return tableItems.Length; 
      } 

      /// <summary> 
      /// Called when a row is touched 
      /// </summary> 

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

      /// <summary> 
      /// Called by the TableView to get the actual UITableViewCell to render for the particular row 
      /// </summary> 
      public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) 
      { 
       // request a recycled cell to save memory 
       UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier); 
       // if there are no cells to reuse, create a new one 
       if (cell == null) 
        cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier); 

       cell.TextLabel.Text = tableItems[indexPath.Row]; 

       return cell; 
      } 
     } 

     protected class BasicMapAnnotation : MKAnnotation 
     { 
      /// <summary> 
      /// The location of the annotation 
      /// </summary> 
      public override CLLocationCoordinate2D Coordinate { get; set; } 
      protected string title; 
      protected string subtitle; 

      /// <summary> 
      /// The title text 
      /// </summary> 
      public override string Title 
      { get { return title; } } 


      /// <summary> 
      /// The subtitle text 
      /// </summary> 
      public override string Subtitle 
      { get { return subtitle; } } 

      public BasicMapAnnotation (CLLocationCoordinate2D coordinate, string title, string subTitle) 
       : base() 
      { 
       this.Coordinate = coordinate; 
       this.title = title; 
       this.subtitle = subTitle; 
      } 
     } 

    } 
} 
+0

Опубликовать полную трассировку стека – Cheesebaron

ответ

0

вы не должны создавать новый MainRightViewController в вашем экземпляре TableSource, но передать его экземпляр вы вызываете его с.

public partial class MainRightViewController : UIViewController 
{ 
    (...) 

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

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

и в вашем UITableViewSource:

public class TableSource : UITableViewSource { 

     public MainRightViewController parentController; 

     public TableSource (string[] items, MainRightViewController parentController) 
     { 
      tableItems = items; 
      this.parentController = parentController 
     } 

Я надеюсь, что это поможет вам получить происходит.

+0

... хотя я ответил как 10 месяцев спустя ;-) –

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