开发者

Searching a TableView of annotations

I had no idea how to set up a database when i started so i have 80 annotations that all look like this

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];

They are grouped into 22 cities through the MainViewAnnotationType, which is coded as follows:

开发者_如何学JAVA- (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;
}

The annotations are then put into a TableView like so;

- (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];
        }
    }   
}

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.

New to Xcode, any help including some code examples would be much appreciated.


What I did is to create my UITableView not programmatically but using Interface Builder. Create your own custom ViewController and add inside an UITableView. Don't forget to set (by interface builder) delegate and datasource. Afterwards I think it might be easier for you to manage your hidden/show search bar. Hope this can help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜