搜索城市列表

时间:2012-06-28 01:25:29

标签: iphone ios database uitableview

任何人都可以就如何做到这一点向我说明正确的方向:

enter image description here

来自天气频道的此示例显示了我想要做的事情。我只想要一个有人可以搜索城市的桌面视图。我不知道从哪里获得这些资源以及如何做到这一点。

2 个答案:

答案 0 :(得分:2)

您可以从MaxMind找到世界城市列表。

它是您正在寻找的资源吗?

答案 1 :(得分:1)

创建表格视图和搜索栏。您必须实施他们的代理人UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar1
{

    searchBar.showsSearchResultsButton = YES;
    searchBar.showsCancelButton = YES;
    searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
    // flush the previous search content
    //Implement some code
 }
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar1
{

     searchBar.showsCancelButton = NO;
    searchBar.showsSearchResultsButton = NO;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{

    if([searchText isEqualToString:@""]||searchText==nil){
        [yourTable reloadData];
        return;
    }
    NSInteger counter = 0;
    for(NSString *name in yourArray)
    {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
        NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];
      //  NSRange r = [name rangeOfString:searchText];
      //  NSRange r = [name  ];
        if(r.location != NSNotFound)
        {
           //Implement the code.
        }
        counter++;
        [pool release];
    }
    [yourTable reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar1
{

    // if a valid search was entered but the user wanted to cancel, bring back the main list content
    // Implement some code
    @try{
        [yourTable reloadData];
    }
    @catch(NSException *e){
    }
    [searchBar resignFirstResponder];
    searchBar.text = @"";
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar1
{

    [searchBar1 resignFirstResponder];
}

我已经给你不完整的方法,你可以实现你想做的事。

我认为这会对你有所帮助。