效果圖.gif
<h5>開發(fā)準(zhǔn)備</h5>獲取城市的信息,我這里是從網(wǎng)上搜的一個(gè)plist文件,如果信息不全大家直接可以在里面補(bǔ)充自己所需的信息.圖1是表結(jié)構(gòu)(這種結(jié)構(gòu)可以進(jìn)行多種模式的搜索)
圖1 表結(jié)構(gòu).png
<h5>城市選擇器分析</h5>
城市選擇器器思維導(dǎo)圖.png
從界面直觀的分析,這個(gè)城市選擇器主要是用UITableView,搜索框撩嚼、實(shí)現(xiàn)的,主要的工作在Cell的自定義里面:定位城市+熱門城市是一種類型的Cell,顯示城市有事一種類型,我們也可以直接用系統(tǒng)的 Cell,整個(gè)tableView的組頭類型和顯示都是一樣我們定義一種組頭即可,點(diǎn)擊搜索你可以輸入拼音,簡寫,和簡拼,這時(shí)會彈出一個(gè)灰色透明的UItableVIewCOntroller,搜索的結(jié)果也會顯示出來
<h5>使用方法</h5>
<li>下載HZCityPicker儿礼,將Demo中的CityPicker文件夾拖到你的項(xiàng)目中麻裳。</li>
<li>導(dǎo)入頭文件,代理</li>
#import "CityPickerController.h"
@interface ViewController ()<CityPickerDelegate>
<p></p>
<h5>字母索引:</h5>
//添加索引
- (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.arraySection;
}
//索引點(diǎn)擊
- (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>導(dǎo)入頭文件 </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文件