iOS城市选择器

作者: 薄阳映初雪 | 来源:发表于2017-07-14 18:32 被阅读0次
效果图.gif

<h5>开发准备</h5>获取城市的信息,我这里是从网上搜的一个plist文件,如果信息不全大家直接可以在里面补充自己所需的信息.图1是表结构(这种结构可以进行多种模式的搜索)


图1 表结构.png

<h5>城市选择器分析</h5>

城市选择器器思维导图.png
从界面直观的分析,这个城市选择器主要是用UITableView,搜索框、实现的,主要的工作在Cell的自定义里面:定位城市+热门城市是一种类型的Cell,显示城市有事一种类型,我们也可以直接用系统的 Cell,整个tableView的组头类型和显示都是一样我们定义一种组头即可,点击搜索你可以输入拼音,简写,和简拼,这时会弹出一个灰色透明的UItableVIewCOntroller,搜索的结果也会显示出来
<h5>使用方法</h5>
<li>下载HZCityPicker,将Demo中的CityPicker文件夹拖到你的项目中。</li>
<li>导入头文件,代理</li>
#import "CityPickerController.h"
@interface ViewController ()<CityPickerDelegate>

<p></p>
<h5>字母索引:</h5>

//添加索引
- (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.arraySection;
}
//索引点击
- (NSInteger) tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    if(index == 0) {
        [self.tableView scrollRectToVisible:self.searchController.searchBar.frame animated:NO];
        return -1;
    }
    return index - 1;
}

<h5>UISearchBar搜索框:</h5>

#pragma mark - UISearchResultsUpdating
- (void) updateSearchResultsForSearchController:(UISearchController *)searchController
{
    //自定义中文取消
    searchController.searchBar.showsCancelButton = YES;
    UIButton *canceLBtn = [searchController.searchBar valueForKey:@"cancelButton"];
    [canceLBtn setTitle:@"取消" forState:UIControlStateNormal];
    [canceLBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    
    NSString *searchText = searchController.searchBar.text;
    [self.data removeAllObjects];
    for (CityModel *city in self.cityData){
        if ([city.cityName containsString:searchText] || [city.pinyin containsString:searchText] || [city.initials containsString:searchText]) {
            [self.data addObject:city];
        }
    }
    [self.tableView reloadData];
}
```
<h5>Cell上的Btn</h5>
```
- (void) setCityArray:(NSArray *)cityArray
{
    _cityArray = cityArray;
    [self.noDataLabel setHidden:(cityArray != nil && cityArray.count > 0)];
    
    for (int i = 0; i < cityArray.count; i ++) {
        CityModel *city = [cityArray objectAtIndex:i];
        UIButton *button = nil;
        if (i < self.arrayCityButtons.count) {
            button = [self.arrayCityButtons objectAtIndex:i];
        }
        else {
            button = [[UIButton alloc] init];
            [button setBackgroundColor:[UIColor whiteColor]];
            [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [button.titleLabel setFont:[UIFont systemFontOfSize:14.0f]];
            [button.layer setMasksToBounds:YES];
            [button.layer setCornerRadius:2.0f];
            [button.layer setBorderColor:[UIColor colorWithWhite:0.8 alpha:1.0].CGColor];
            [button.layer setBorderWidth:1.0f];
            [button addTarget:self action:@selector(cityButtonDown:) forControlEvents:UIControlEventTouchUpInside];
            [self.arrayCityButtons addObject:button];
            [self addSubview:button];
        }
        [button setTitle:city.cityName forState:UIControlStateNormal];
        button.tag = i;
    }
    while (cityArray.count < self.arrayCityButtons.count) {
        [self.arrayCityButtons removeLastObject];
    }
}
```
<h5>HZLocation定位</h5>如果想修改定位可以在HZLocation文件中修改
<li>导入头文件 </li>
```
#import "HZLocation.h"

```
<li>定位代理</li>
```
//定位中...
- (void)locating {
    NSLog(@"定位中...");
}

//定位成功
- (void)currentLocation:(NSDictionary *)locationDictionary {
    NSString *city = [locationDictionary valueForKey:@"City"];
   
    if (![_cityLabel.text isEqualToString:city]) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:[NSString stringWithFormat:@"您定位到%@,确定切换城市吗?",city] preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            _cityLabel.text = city;

        }];
        [alertController addAction:cancelAction];
        [alertController addAction:okAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    
    
}

/// 拒绝定位
- (void)refuseToUsePositioningSystem:(NSString *)message {
    NSLog(@"%@",message);
}

/// 定位失败
- (void)locateFailure:(NSString *)message {
    NSLog(@"%@",message);
}

```

具体使用看[HZCityPicker](https://github.com/HZJason/HZCityPicker),ViewController.m文件

相关文章

网友评论

    本文标题:iOS城市选择器

    本文链接:https://www.haomeiwen.com/subject/cchohxtx.html