我正在尝试使用表格视图添加注释。添加了初始注释,但每当我单击我的表视图以调用其他位置时。它给了我一个错误(nullexception)。 有人可以帮帮我吗?
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 :(得分:0)
你不应该在TableSource实例中创建一个新的MainRightViewController,而是将它从你调用它的实例传递给它。
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
}
我希望这有助于你继续前进。