2012-12-04 67 views
0

在我的應用程序中,我有一個地圖,允許用戶將引腳放到地圖上。這些引腳對地址進行地址解析並將地址設置爲它們的標題。我這樣做是爲了使註釋在被丟棄時存儲在數組中。我有一個tableview,我想在單元格中顯示地址。我在tableview類中有一個指向第一個視圖控制器中的數組的數組,我不知何故需要訪問數組中的地址並將它們設置爲單元格的文本標籤。我已經有了表格視圖顯示與地圖上的引腳一樣多的單元格,但我不知道如何從數組中訪問我需要的信息。這裏有一些代碼可以幫助。如何設置UITableView單元格的文本標籤

這是第一個視圖控制器:

@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; 
} 

所以基本上我想在陣列中註釋的標題是細胞中爲textLabel。因此,如果數組中有5個引腳,我希望5個單元將註釋的相應地址(title屬性)作爲文本標籤。我將不勝感激。

回答

2

之前return cell;,你可以做cell.textLabel.text = [_pinArray objectAtIndex:indexPath.row]確保你從_pinArray中提取標題屬性。

+0

謝謝你!它工作完美。 –

+1

@ChandlerDeAngelis FYI:單元格中還有詳細的文本標籤,用於標題的任何細節。 cell.detailTextLabel.text。 – rptwsthi

+0

@rptwsthi好的謝謝你的擡頭。 –

相關問題