Note that there are some explanatory texts on larger screens.

plurals
  1. POSearching a TableView of annotations
    primarykey
    data
    text
    <p>I had no idea how to set up a database when i started so i have 80 annotations that all look like this</p> <pre><code>workingCoordinate.latitude = 50.795825; workingCoordinate.longitude = -1.103139; MainViewAnnotation *Levant = [[MainViewAnnotation alloc] initWithCoordinate:workingCoordinate]; [Levant setTitle:@"Levant"]; [Levant setSubtitle:@"Unit R09, Gunwharf Quays"]; [Levant setAnnotationType:MainViewAnnotationTypePortsmouth]; [mapView addAnnotation:Levant]; </code></pre> <p>They are grouped into 22 cities through the MainViewAnnotationType, which is coded as follows:</p> <pre><code>- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 21; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if(section == MainViewAnnotationTypeBirmingham) { return @"Birmingham"; } else if(section == MainViewAnnotationTypePortsmouth) { return @"Portsmouth"; } return nil; } </code></pre> <p>The annotations are then put into a TableView like so;</p> <pre><code>- (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] autorelease]; } NSMutableArray *annotations = [[NSMutableArray alloc] init]; if(indexPath.section == 0) { for(MainViewAnnotation *annotation in [mapView annotations]) { if(![annotation isKindOfClass:[MKUserLocation class]]) { if([annotation annotationType] == MainViewAnnotationTypeBirmingham) { [annotations addObject:annotation]; } } } cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title]; } ... else if(indexPath.section == 17) { for(MainViewAnnotation *annotation in [mapView annotations]) { if(![annotation isKindOfClass:[MKUserLocation class]]) { if([annotation annotationType] == MainViewAnnotationTypePortsmouth) { [annotations addObject:annotation]; } } } cell.textLabel.text = [[annotations objectAtIndex:indexPath.row] title]; } return cell; } and then when a cell is clicked, will zoom in on the annotation. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { for(MainViewAnnotation *annotation in [mapView annotations]) { if([[[[tableView cellForRowAtIndexPath:indexPath] textLabel] text] isEqualToString:[annotation title]]) { [mapView setRegion:MKCoordinateRegionMake([annotation coordinate], MKCoordinateSpanMake(.003, .003)) animated:YES]; } } } </code></pre> <p>My question is, I would like to be able to search my annotations either through their city their in or by their annotation name. I have a search bar controller already in my interface and it has been connected to my tableview, just not written any code. I would like the search to be hidden unless i click a search button then hide when i click on a cell.</p> <p>New to Xcode, any help including some code examples would be much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload