在我的应用中,我有一张地图,允许用户将引脚放到地图上。引脚对地址进行地理编码并将地址设置为标题。我做了它,以便注释在被删除时存储在数组中。我有一个tableview,我想在单元格中显示地址。我在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个单元格具有相应的注释地址(title属性)作为textlabels。我很感激你的帮助。
答案 0 :(得分:2)
在return cell;
之前,您可以执行cell.textLabel.text = [_pinArray objectAtIndex:indexPath.row]
确保从_pinArray中提取title属性。