2013-06-20 30 views
-4

我有一個類的地圖視圖,其中包含標題和副標題的所有註釋。有一個導航欄按鈕,顯示所有這些標題和字幕的列表。我希望能夠在表格視圖中選擇一行,並在地圖視圖中打開具有相同標題的註釋。任何幫助?從表格視圖中選定行打開地圖註釋

我現在唯一的問題是獲取地圖視圖打開時顯示的註釋。我知道我需要使用selectAnnotation:animated:但我無法弄清楚如何告訴它打開匹配標題的註釋。

在RSFM(名爲世界觀地圖視圖類),我告訴像這樣的表視圖:

- (void)showList 
{ 
    List *list = [[List alloc] initWithNibName:nil bundle:nil]; 
    list.annotations = [[NSArray alloc]initWithArray:worldView.annotations]; 
    list.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
    [self.navigationController pushViewController:list animated:YES]; 
} 

這裏是我在我的didSelectRowAtIndexPath方法方法,至今在我的表視圖:

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

     RSFM *rsfm = [[RSFM alloc] initWithNibName:nil bundle:nil]; 

     NSLog(@"List Annotations Array Count: %d", [annotations count]); 

     UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; 
     NSString *cellTitle = selectedCell.textLabel.text; 
     NSLog(@"Cell Title: %@", cellTitle); 


     for (NSUInteger i = 0; i < [annotations count]; i++) 
     { 
      NSString *stringFromArray = [[annotations objectAtIndex:i]title]; 
      NSLog(@"Annotations string: %@", stringFromArray); 

      if ([cellTitle isEqualToString:stringFromArray]) 
      { 
       RSFM *rsfm = [[RSFM alloc] initWithNibName:nil bundle:nil]; 
       rsfm.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
       [self.navigationController pushViewController:rsfm animated:YES]; 

       // [rsfm.worldView selectAnnotation:<#(id<MKAnnotation>)#> animated:YES]; 
      } 
     } 

     [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    } 

回答

3

當您選擇在tableView一排,你的方法tableView:didSelectRowAtIndexPath:被調用。所以你必須使用indexPath來讀取模型中的數據,並獲得標題。
然後,您可以使用annotations屬性從mapView中讀取所有註釋。在獲得的數組中,掃描對象直到找到具有相同標題的註釋。然後你可以發送你的mapView消息selectAnnotation:animated:,它會被顯示。
請注意,這個問題有更多聰明的方法,但首先嚐試它應該工作。

+0

我想從我的地圖視圖獲得註釋的陣列,但在陣列中的項目數爲0 - (空)的tableView:(UITableView的*)aTableView didSelectRowAtIndexPath方法:(NSIndexPath *)indexPath { RSFM * rsfm = [[RSFM alloc] init]; NSArray * annotations = [NSArray arrayWithObjects:rsfm.worldView.annotations,nil]; NSLog(@「註釋數量:%d」,[註釋數量]); } – raginggoat

+0

我解決了這個問題,但我不太清楚如何看僅註釋標題。 – raginggoat

+0

我已更新我的原始帖子。 – raginggoat

0

我無法將此添加到我的原始文章,因爲它太長,所以我在這裏發佈它。

這裏是我的表視圖:

#import "List.h" 
#import "RSFM.h" 
#import "DTCustomColoredAccessory.h" 

@interface List() 

@end 

@implementation List 
{ 
    NSMutableArray *title; 
    NSMutableArray *subtitle; 
    NSMutableArray *displayItems; 
    NSMutableDictionary *marketDictionary; 
    NSMutableArray *farmMarkets; 
    NSArray *keys; 
    NSMutableArray *objects; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    title = [[NSMutableArray alloc]initWithObjects:@"Cates Farm", @"Broadbent B & B Foods", @"Cayce's Pumpkin Patch", @"Metcalfe Landscaping", @"Brumfield Farm Market", @"Dogwood Valley Farm", @"Country Fresh Meats & Farmers Market", @"Jim David Meats", @"Trunnell's Farm Market", @"Lovell's Orchard & Farm Market", @"Zook's Produce", @"The Country Barn", @"Poore's Nursery & Farms", @"Just Piddlin Farm", @"Chaney's Dairy Barn & Restaurant", @"Jackson's Orchard & Nursery, Inc.", @"Mammoth Cave Transplants", @"Habegger's Amish Market", @"Kenny's Farmhouse Cheese", @"Dennison's Roadside Market", @"Roberts Family Farm", @"Wooden Farm", @"Lee's Garden Center, Florist & Gift Shop", @"Hinton's Orchard & Farm Market", @"Serenity Farm Alpacas", @"Burton's Nursery & Garden Center", @"Davis Family Farms", @"Heavenly Haven Farm", @"French Valley Farms", @"Cravens Greenhouse", @"Haney's Appledale Farm", @"Hettmansperger's Greenhouse", @"D & F Farms", @"Double Hart Farm", @"Owens Garden Center", @"Hail's Farm", @"Sinking Valley Vineyard & Winery, Inc.", @"Todd's Greenhouse & Florist, LLC", @"McQuerry's Family Farm-Herbs-N-Heirlooms", @"Berea College Farm & Gardens", @"Acres of Land Winery & Restaurant", @"Baldwin Farms", @"Wonder of Life Farm", @"Chateau du Vieux Corbeau Winery/Old Crow Farm Winery", @"Devine's Farm & Corn Maze", @"Williams Country Market", @"Serano Alpacas & Yarns", @"St. Catharine Farm", @"Capture Your Heart Alpacas", @"Ridgeview Greenhouse & Nursery", @"Country Corner Greenhouse & Nursery, Inc", @"Sunny Acres Farm", @"Morrison's Greenhouses", @"George Gagel Farm Market, LLC", @"Thieneman's Herbs & Perennials", @"Tower View Farm & Nursery", @"Gallrein Farms", @"Sweet Home Spun in the Low Dutch Meetinghouse", @"Mulberry Orchard, LLC", @"Gregory Farms", @"Sherwood Acres Beef", @"Bray Orchard & Roadside Market", @"Callis Orchards", @"Bray Fruit", @"Wilson's Nursery", @"Triple J Farm", @"Ayres Family Orchard", @"Michels Family Farm", @"Amerson Farm", @"Bi-Water Farm & Greenhouse", @"Alpine Hills Dairy Tour/Country Pumpkins", @"Blue Ribbon Market", @"Eagle Bend Alpacas Fiber & Gift Shoppe", @"Redman's Farm",@"The Greenhouse in Gertrude", @"Croppers Greenhouse & Nursery", @"McLean's Aerofresh Fruit", @"Julie's Pumpkins", @"Reed Valley Orchard", @"Evans Orchard & Cider Mill", @"Antioch Daylily Garden", @"Golden Apple Fruit Market", @"Boyd Orchards", @"Serenity Hill Fiber & Living History Farm", @"Beech Springs Farm Market", @"Yuletide Tree Farm & Nursery", @"Townsend's Sorghum Mill and Farm Market", @"Bramble Ridge Orchard", @"Country Garden Greenhouse", @"Golden Apple Fruit Market", @"Black Barn Produce, LLC", @"Imel's Greenhouse", @"Feathered Wing Farm Market", @"Hutton-Loyd Tree Farm", @"Halcomb's Knob, LLC", @"Happy Hollow Farms", @"Reid's Orchard", @"McKinney Farm", @"Crawford Farms", @"Brian T. Guffey Livestock & Produce", @"MeadowBrook Orchards & Farm", @"Rising Sons Home Farm Winery", @"VanMeter Family Farm", nil]; 

    subtitle = [[NSMutableArray alloc]initWithObjects:@"Hwy 425 Henderson, KY 42420", @"257 Mary Blue Road Kuttawa, KY 42055", @"153 Farmersville Road Princeton, KY 42445", @"410 Princeton Road Madisonville, KY 42431", @"3320 Nebo Road Madisonville, KY 42431", @"4551 State Route 109N Clay, KY 42404", @"9355 US Hwy 60 W Sturgis, KY 42459",@"350 T. Frank Wathen Rd. Uniontown, KY 42461", @"9255 Hwy 431 Utica, KY 42376", @"22850 Coal Creek Road Hopkinsville, KY 42240", @"Intersection of KY107 & KY117 Herndon, KY 42240", @"112 Britmart Road Elkton, KY 42220", @"5486 Morgantown Road Russellville, KY 42276", @"10830 S. Morgantown Rd. Woodburn, KY 42170", @"9191 Nashville Road, Bowling Green, KY 42101", @"1280 Slim Island Road Bowling Green, KY 42101", @"5394 Brownsville Road Brownsville, KY 42210", @"945 Perrytown Road Scottsville, KY 42164", @"2033 Thomerson Park Road Austin, KY 42123", @"5824 S. Jackson Hwy. Horse Cave, KY 42749", @"125 Kennedy Road Guston, KY 40142", @"1869 Wooden Lane Elizabethtown, KY 42701", @"1918 Bardstown Road Hodgenville, KY 42748", @"8631 Campbellsville Road Hodgenville, KY 42748", @"1380 Frogg Lane Raywick, KY 40060", @"2212 Saloma Road Campbellsville, KY 42718", @"313 Hwy 1464 Greensburg, KY 42743", @"230 Heavenly Lane Columbia, KY 42728", @"1842 N. Main St. Jamestown, KY 42629", @"500 Cedar Hill Road Albany, KY 42602", @"8350 West 80 Nancy, KY 42544-8756", @"3917 N. Hwy 837 Science Hill, KY 42553", @"755 Elihu Rush Branch Road Somerset, KY 42501", @"6550 Cumberland Falls Road Corbin, KY 40701", @"735 Latham Road Somerset, KY 42503", @"Hwy 461, at 3 mile marker Somerset, KY 42503", @"1300 Plato-Vanhook Road Somerset, KY 42503", @"35 Skyline Drive Eubank, KY 42567", @"169 Pine Hill Road Paint Lick, KY 40461", @"230 N. Main St. Berea, KY 40404", @"2285 Barnes Mill Road Richmond, KY 40475", @"1113 Tates Creek Road Richmond, KY 40475", @"686 Buckeye Road Lancaster, KY 40444", @"471 Stanford Avenue Danville, KY 40422-1927", @"623 Talmage-Mayo Road Harrodsburg, KY 40330", @"4189 Craintown Rd. Gravel Switch, KY 40328", @"1805 Booker Road Springfield, KY 40069", @"2645 Bardstown Road Springfield, KY 40061", @"9430 Bloomfield Road Bloomfield, KY 40008", @"460 Buffalo Run Road Shepherdsville, KY 40165", @"4877 Hwy 44E Shepherdsville, KY 40165", @"6516 Echo Trail Jeffersontown, KY 40299", @"5613 Cooper Chapel Road Louisville, KY 40229", @"2400 Lower Hunters Trace Louisville, KY 40216", @"9120 Blowing Tree Road Louisville, KY 40220", @"12523 Taylorsville Road Jeffersontown, KY 40299", @"1029 Vigo Road Shelbyville, KY 40065", @"6805 Castle Hwy. Pleasureville, KY 40057", @"1330 Mulberry Pike Shelbyville, KY 40065", @"985 Vance Road Turners Station, KY 40075", @"215 Parker Drive LaGrange, KY 40031", @"2580 Hwy 42 W. Bedford, KY 40006", @"3721 Hwy 421 N Bedford, KY 40006", @"1660 Highway 421 N Bedford, KY 40006", @"3690 East-West Connector (Rte 676) Frankfort, KY 40601", @"2287 Long Lick Road Georgetown, KY 40324", @"525 Wilson Lane Owenton, KY 40359", @"4275 Hwy 1316 Sparta, KY 41086", @"130 McClelland Circle Georgetown, KY 40324", @"877 Cincinnati Road Georgetown, KY 40324", @"2165 Sherman Mount Zion Rd. Dry Ridge, KY 41035", @"8707 Camp Ernst Road Union, KY 41091", @"7812 East Bend Road Burlington, KY 41005", @"12449 Decoursey Pike Morning View, KY 41063", @"3246 Augusta-Berlin Road Brooksville, KY 41004", @"5350 Raymond Road May's Lick, KY 41055", @"4085 Ewing Road Ewing, KY 41039", @"1069 Ruddles Mill Road Paris, KY 40361", @"239 Lail Lane Paris, KY 40361", @"180 Stone Road Georgetown, KY 40324", @"2231 Houston Antioch Road Lexington, KY 40516", @"1801 Alexandria Drive Lexington, KY 40504", @"1396 Pinckard Pike Versailles, KY 40383", @"1371 Beverly Lane Nicholasville, KY 40356", @"4776 Old Boonesboro Road Winchester, KY 40391", @"3925 Old Boonesboro Road Winchester, KY 40391", @"11620 Main Street Jeffersonville, KY 40337", @"2726 Osborne Road Mt. Sterling, KY 40353", @"99 Union Road Beattyville, KY 41311", @"1523 Hwy 119 North Whitesburg, KY 41815", @"52 KY Route 3224 River, KY 41254", @"2836 State Route 1 Greenup, KY 41144", @"45 Katherine Lane Greenup, KY 41144", @"1483 Big Run Road Wallingford, KY 41093", @"430 Wallacetown Road Paint Lick, KY 40461", @"9730 KY 136W Calhoun, KY 42327", @"4818 Hwy 144 Owensboro, KY 42303", @"88 Noe Lane Russellville, KY 42276", @"59 Williams Cemetery Rd. Hodgenville, KY 42748", @"1114 KY Hwy 829 Albany, KY 42602", @"680 Dug Hill Rd. Elk Horn, KY 42733", @"975 Frankfort Rd. Lawrenceburg, KY 40342", @"164 Old Peonia Loop Clarkson, KY 42726", nil]; 

    // searchBar.showsCancelButton = YES; 
    searchBar.placeholder = @"Search for name or address"; 

    farmMarkets = [[NSMutableArray alloc]init]; 

    for (NSUInteger i = 0; i < [title count]; i++) 
    { 
     [farmMarkets addObject: @{@"title" : [title objectAtIndex:i], @"subtitle" : [subtitle objectAtIndex:i]}]; 
    } 

    displayItems = [[NSMutableArray alloc]initWithArray:farmMarkets]; 

    // [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardShown:) name:UIKeyboardDidShowNotification object:nil]; 
    // [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardHidden:) name:UIKeyboardWillHideNotification object:nil]; 
} 

/* 
- (void)keyboardShown:(NSNotification *)note 
{ 
    CGRect keyboardFrame; 
    [[[note userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey]getValue:&keyboardFrame]; 
    CGRect tableViewFrame = tableView.frame; 
    tableViewFrame.size.height -= keyboardFrame.size.height; 
    [tableView setFrame:tableViewFrame]; 
} 

- (void)keyboardHidden:(NSNotification *)note 
{ 
    [tableView setFrame:self.view.bounds]; 
} 
*/ 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // return [title count]; 
    return [displayItems count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // static NSString *mbTableIdentifier = @"SimpleTableItem"; 
    static NSString *marketListIdentifier = @"SimpleTableItem"; 
    UIImageView *image = [[UIImageView alloc]init]; 
    image.image = [UIImage imageNamed:@"CellImage.png"]; 

    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mbTableIdentifier]; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:marketListIdentifier]; 

    if (cell == nil) 
    { 
     // cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:mbTableIdentifier]; 
     // cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:marketListIdentifier]; 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:marketListIdentifier]; 
     // cell.textLabel.font=[UIFont systemFontOfSize:16.0]; 
    } 

    // cell.backgroundView = [[CustomCellBackground alloc] init]; 
    // cell.selectedBackgroundView = [[CustomCellBackground alloc] init]; 
    cell.textLabel.font = [UIFont fontWithName:@"FranklinGothicStd-ExtraCond" size:20.0]; 
    cell.textLabel.backgroundColor = [UIColor clearColor]; 
    cell.textLabel.highlightedTextColor = [UIColor darkGrayColor]; 
    cell.textLabel.textColor = [UIColor whiteColor]; 
    cell.detailTextLabel.font = [UIFont fontWithName:@"FranklinGothicStd-ExtraCond" size:14.0]; 
    cell.detailTextLabel.backgroundColor = [UIColor clearColor]; 
    cell.detailTextLabel.highlightedTextColor = [UIColor darkGrayColor]; 
    cell.detailTextLabel.textColor = [UIColor whiteColor]; 

    cell.backgroundView = image; 

    // NSLog(@"cell separator style: %d separator color: %@", tableView.separatorStyle, tableView.separatorColor); 

    cell.textLabel.text = [[displayItems objectAtIndex:indexPath.row]objectForKey:@"title"]; 
    cell.detailTextLabel.text = [[displayItems objectAtIndex:indexPath.row]objectForKey:@"subtitle"]; 

    DTCustomColoredAccessory *accessory = [DTCustomColoredAccessory accessoryWithColor:cell.textLabel.textColor]; 
    accessory.highlightedColor = [UIColor darkGrayColor]; 
    cell.accessoryView =accessory; 

    return cell; 
} 

- (void)searchBarTextDidBeginEditing:(UISearchBar *)aSearchBar 
{ 
    searchBar.showsCancelButton = YES; 
} 

- (void)searchBarTextDidEndEditing:(UISearchBar *)aSearchBar 
{ 
    searchBar.showsCancelButton = NO; 
} 

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
{ 
    if ([searchText length] == 0) 
    { 
     [displayItems removeAllObjects]; 
     [displayItems addObjectsFromArray:title]; 
    } 
    else 
    { 
     [displayItems removeAllObjects]; 
     for (NSString *string in title) 
     { 
      NSRange r = [string rangeOfString:searchText options:NSCaseInsensitiveSearch]; 
      if (r.location != NSNotFound) 
      { 
       [displayItems addObject:string]; 
      } 
     } 
    } 
    [tableView reloadData]; 
} 

- (void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar 
{ 
    [searchBar resignFirstResponder]; 
} 

- (void)searchBarCancelButtonClicked:(UISearchBar *)aSearchBar 
{ 
    [searchBar resignFirstResponder]; 
} 

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 
+0

您能否專注於該問題?我們無法調試您的整個程序。 –

+0

我只是不確定如何在地圖視圖中打開具有相同標題的註釋,並在表格視圖中具有選定的單元格。 – raginggoat

+0

我不想找人給我答案,但我想要一些幫助。 – raginggoat

1

我解決了問題,但實際上最終決定從表格視圖中顯示選定項目的詳細視圖,而不是在地圖上顯示該註釋。

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@"List Annotations Array Count: %d", [annotations count]); 

    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath]; 
    NSString *cellTitle = selectedCell.textLabel.text; 
    NSString *cellSubtitle = selectedCell.detailTextLabel.text; 

    for (NSUInteger i = 0; i < [annotations count]; i++) 
    { 
     NSString *stringFromArray = [[annotations objectAtIndex:i]title]; 

     if ([cellTitle isEqualToString:stringFromArray]) 
     { 
      id<MKAnnotation> ann = [annotations objectAtIndex:i]; 
      AnnotationDetailView *detail = [[AnnotationDetailView alloc]init]; 

      NSLog(@"Cell Title: %@", cellTitle); 
      NSLog(@"Cell Title: %@", cellSubtitle); 
      detail.latText = [NSString localizedStringWithFormat:@"%f",ann.coordinate.latitude]; 
      detail.longText = [NSString localizedStringWithFormat:@"%f", ann.coordinate.longitude]; 
      detail.annTitle = cellTitle; 
      detail.annSub = cellSubtitle; 

      detail.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
      [self.navigationController pushViewController:detail animated:YES]; 
     } 
    } 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
} 
相關問題