0

私のアプリには、ユーザーがピンをマップにドロップできるマップがあります。ピンは住所をジオコーディングし、住所をタイトルとして設定します。アノテーションがドロップされたときに配列に格納されるようにしました。セルにアドレスを表示したいテーブルビューがあります。最初のView Controllerの配列を指すtableviewクラスに配列があり、配列からアドレスにアクセスしてセルのテキストラベルとして設定する必要があります。マップ上のピンと同じ数のセルをテーブル ビューに表示していますが、配列から必要な情報にアクセスする方法がわかりません。ここに役立つコードがあります。

最初のView Controllerは次のとおりです。

@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 として持たせます。助けていただければ幸いです。

4

1 に答える 1

2

の前return cell;に、cell.textLabel.text = [_pinArray objectAtIndex:indexPath.row]_pinArray からタイトル プロパティを抽出していることを確認してください。

于 2012-12-04T04:18:35.853 に答える