在我的应用程序中,我有一张地图,允许用户将图钉放到地图上。引脚对地址进行地理编码并将地址设置为其标题。我这样做是为了在删除注释时将它们存储在一个数组中。我有一个表格视图,我想在单元格中显示地址。我在 tableview 类中有一个数组,它指向第一个视图控制器中的数组,我需要以某种方式访问数组中的地址并将它们设置为单元格的文本标签。我已经让表格视图显示了与地图上的引脚一样多的单元格,但我不知道如何从数组中访问我需要的信息。这是一些帮助的代码。
这是第一个视图控制器:
@implementation P2OViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[_worldView setShowsUserLocation:YES];
NSInteger mapTypeValue = [[NSUserDefaults standardUserDefaults]
integerForKey:P2OMapTypePrefKey];
// Update the UI
[mapTypeControl setSelectedSegmentIndex:mapTypeValue];
// Update the map
[self changeMapType:mapTypeControl];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self
action:@selector(press:)];
[longPress setMinimumPressDuration:0.5f]; //user needs to press for 2 seconds
[longPress setDelegate:self];
[_worldView addGestureRecognizer:longPress];
_annotationArray = [[NSMutableArray alloc]init];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]init];
[_worldView addGestureRecognizer:pan];
number = [NSNumber numberWithInt:0];
}
-(void)press:(UILongPressGestureRecognizer *)recognizer
{
CGPoint touchPoint = [recognizer locationInView:_worldView];
CLLocationCoordinate2D touchMapCoordinate = [_worldView convertPoint:touchPoint toCoordinateFromView:_worldView];
geocoder = [[CLGeocoder alloc]init];
CLLocation *location = [[CLLocation alloc]initWithCoordinate:touchMapCoordinate
altitude:CLLocationDistanceMax
horizontalAccuracy:kCLLocationAccuracyBest
verticalAccuracy:kCLLocationAccuracyBest
timestamp:[NSDate date]];
[geocoder reverseGeocodeLocation:location
completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"reverseGeocoder:completionHandler: called");
if (error) {
NSLog(@"Geocoder failed with error: %@", error);
} else {
CLPlacemark *place = [placemarks objectAtIndex:0];
geocodedAddress = [NSString stringWithFormat:@"%@ %@, %@ %@", [place subThoroughfare], [place thoroughfare], [place locality], [place administrativeArea]];
if (UIGestureRecognizerStateBegan == [recognizer state]) {
value = [number intValue];
number = [NSNumber numberWithInt:value + 1];
addressPin = [[MapPoint alloc]initWithAddress:geocodedAddress coordinate:touchMapCoordinate
title:geocodedAddress identifier:number];
NSLog(@"The identifier is %@", number);
[_annotationArray addObject:addressPin];
[_worldView addAnnotation:addressPin];
NSLog(@"The number of pins in the annotation array is: %u",_annotationArray.count);
}
}
}];
}
-(void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)view
calloutAccessoryControlTapped:(UIControl *)control
{
PinViewController *pvc = [[PinViewController alloc]init];
[[self navigationController]pushViewController:pvc
animated:YES];
pvc.label.text = view.annotation.title;
}
- (IBAction)bookmarkPressed:(id)sender
{
P2OTableViewViewController *tvc = [[P2OTableViewViewController alloc]initWithStyle:UITableViewStyleGrouped];
[tvc setValue:_annotationArray];
[[self navigationController]pushViewController:tvc
animated:YES];
}
@end
这是我的 TableViewController
#import <UIKit/UIKit.h>
@class P2OViewController;
@interface P2OTableViewViewController : UITableViewController
{
NSMutableArray *_pinArray;
__weak P2OViewController *_pvc;
}
@property (readonly) UILabel *textLabel;
@property (weak,nonatomic) P2OViewController *pvc;
@property (strong,nonatomic) NSMutableArray *pinArray;
-(void)setValue:(NSMutableArray *)value;
@end
@implementation P2OTableViewViewController
@synthesize pinArray = _pinArray, pvc = _pvc;
- (id)init
{
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
_pinArray = [[NSMutableArray alloc]init];
}
return self;
}
-(void)setValue:(NSMutableArray *)value
{
_pinArray = value;
}
-(id)initWithStyle:(UITableViewStyle)style
{
return [self init];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return _pinArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cell;
}
所以基本上我希望数组中注释的标题成为单元格的文本标签。因此,如果数组中有 5 个引脚,我希望 5 个单元格具有相应的注释地址(标题属性)作为文本标签。我将不胜感激。