本文介绍了搜索TableView的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我开始时我不知道如何设置数据库所以我有80个注释都看起来像这样

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

它们通过MainViewAnnotationType分组到22个城市,编码如下:

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

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

然后将注释放入TableView中;

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

我的问题是,我会喜欢能够通过他们的城市或他们的注释名称来搜索我的注释。我的界面中已经有一个搜索栏控制器,它已连接到我的tableview,只是没有编写任何代码。
我想隐藏搜索,除非我点击一个搜索按钮然后在我点击一个单元格时隐藏。

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.

Xcode的新功能,包括一些帮助代码示例将非常感激。

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

推荐答案

我所做的是不是以编程方式创建我的UITableView,而是使用Interface Builder。创建自己的自定义ViewController并在UITableView中添加。不要忘记设置(通过接口构建器)委托和数据源。之后我认为您可能更容易管理隐藏/显示搜索栏。
希望这可以提供帮助。

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.

这篇关于搜索TableView的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:36
查看更多