iOS城市選擇器

效果圖.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文件

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末征唬,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子茁彭,更是在濱河造成了極大的恐慌总寒,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,607評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件理肺,死亡現(xiàn)場離奇詭異摄闸,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)妹萨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評論 3 395
  • 文/潘曉璐 我一進(jìn)店門年枕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人乎完,你說我怎么就攤上這事熏兄。” “怎么了?”我有些...
    開封第一講書人閱讀 164,960評論 0 355
  • 文/不壞的土叔 我叫張陵摩桶,是天一觀的道長桥状。 經(jīng)常有香客問我,道長硝清,這世上最難降的妖魔是什么辅斟? 我笑而不...
    開封第一講書人閱讀 58,750評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮芦拿,結(jié)果婚禮上士飒,老公的妹妹穿的比我還像新娘。我一直安慰自己蔗崎,他們只是感情好酵幕,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,764評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著缓苛,像睡著了一般裙盾。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上他嫡,一...
    開封第一講書人閱讀 51,604評論 1 305
  • 那天,我揣著相機(jī)與錄音庐完,去河邊找鬼钢属。 笑死,一個(gè)胖子當(dāng)著我的面吹牛门躯,可吹牛的內(nèi)容都是我干的淆党。 我是一名探鬼主播,決...
    沈念sama閱讀 40,347評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼讶凉,長吁一口氣:“原來是場噩夢啊……” “哼染乌!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起懂讯,我...
    開封第一講書人閱讀 39,253評論 0 276
  • 序言:老撾萬榮一對情侶失蹤荷憋,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后褐望,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體勒庄,經(jīng)...
    沈念sama閱讀 45,702評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,893評論 3 336
  • 正文 我和宋清朗相戀三年瘫里,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了实蔽。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,015評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡谨读,死狀恐怖局装,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤铐尚,帶...
    沈念sama閱讀 35,734評論 5 346
  • 正文 年R本政府宣布拨脉,位于F島的核電站,受9級特大地震影響塑径,放射性物質(zhì)發(fā)生泄漏女坑。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,352評論 3 330
  • 文/蒙蒙 一统舀、第九天 我趴在偏房一處隱蔽的房頂上張望匆骗。 院中可真熱鬧,春花似錦、人聲如沸伟墙。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽瓮钥。三九已至,卻和暖如春烹吵,著一層夾襖步出監(jiān)牢的瞬間碉熄,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評論 1 270
  • 我被黑心中介騙來泰國打工肋拔, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留锈津,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,216評論 3 371
  • 正文 我出身青樓凉蜂,卻偏偏與公主長得像琼梆,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子窿吩,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,969評論 2 355

推薦閱讀更多精彩內(nèi)容