JFCitySelector最開始只是本身學習時寫的demo(請看:iOS-(仿美團)城市選擇器+自動定位+字母索引),寫的很笨重(引入了一些第三方庫,且使用sqlite來做爲數據源),當時找的城市數據也不夠齊全;一直有重寫的計劃,因懶癌晚期一直沒具體實施,直到有次偶然發現了Administrative-divisions-of-China 中華人民共和國行政區劃數據源我才下定決心去完成這個項目,最終有了如今的JFCitySelector:一個輕量、靈活、可自定義的三級城市選擇器。相對於上一個版本,使用.plist做爲數據源代替sqlite,城市名稱拼音已預先處理寫入到數據源.plist裏使用時只作讀取操做,因此會很快;不用在使用Masonry
和 FMDB
因此很輕量。git
JFCSTableViewController
JFCSTableViewControllerDelegate
,initWithConfiguration: delegate:
初始化JFCSTableViewController
;若直接使用 init
初始化JFCSTableViewController
,JFCSConfiguration
則爲默認配置。//自定義配置...
JFCSConfiguration *config = [[JFCSConfiguration alloc] init];
//關閉拼音搜索
config.isPinyinSearch = NO;
//配置熱門城市
config.popularCitiesMutableArray = [self defealtPopularCities];
JFCSTableViewController *vc = [[JFCSTableViewController alloc] initWithConfiguration:config delegate:self];
[self.navigationController pushViewController:vc animated:YES];
#pragma mark -- JFCSTableViewControllerDelegate
- (void)viewController:(JFCSTableViewController *)viewController didSelectCity:(JFCSBaseInfoModel *)model {
//選擇城市後...
NSLog(@"name %@ code %zd pinyin %@ alias %@ firstLetter %@",model.name, model.code, model.pinyin, model.alias, model.firstLetter);
}
//自定義熱門城市
- (NSMutableArray <JFCSPopularCitiesModel *> *)defealtPopularCities {
JFCSPopularCitiesModel *bjModel = [[JFCSPopularCitiesModel alloc] initWithName:@"北京" type:JFCSPopularCitiesTypeCity];
JFCSPopularCitiesModel *shModel = [[JFCSPopularCitiesModel alloc] initWithName:@"上海" type:JFCSPopularCitiesTypeCity];
JFCSPopularCitiesModel *gzModel = [[JFCSPopularCitiesModel alloc] initWithName:@"廣州" type:JFCSPopularCitiesTypeCity];
JFCSPopularCitiesModel *szModel = [[JFCSPopularCitiesModel alloc] initWithName:@"深圳" type:JFCSPopularCitiesTypeCity];
JFCSPopularCitiesModel *hzModel = [[JFCSPopularCitiesModel alloc] initWithName:@"杭州" type:JFCSPopularCitiesTypeCity];
return [NSMutableArray arrayWithObjects:bjModel, shModel, gzModel, szModel, hzModel, nil];
}
複製代碼
JFCSDataOpreation
提供的數據接口,自建UI。initWithConfiguration:
,傳入JFCSConfiguration
實例,初始化JFCSDataOpreation
JFCSConfiguration *config = [[JFCSConfiguration alloc] init];
config.isPinyinSearch = NO;
//自定義配置...
JFCSDataOpreation *dataOpreation = [[JFCSDataOpreation alloc] initWithConfiguration:config];
[dataOpreation provinces:^(NSArray<JFCSProvince *> * _Nonnull provinces) {
//數據源...
}];
//code...
複製代碼
JFCSConfiguration
的屬性popularCitiesMutableArray
數組,元素必須是JFCSPopularCitiesModel
類型;JFCSPopularCitiesModel
初始化方法:initWithName: type:
,傳入的城市名稱必需要和數據源內的name對應上,由於牽扯到城市名稱所對應的code,因此在自定義城市前能夠先打印所需城市的JFCSBaseInfoModel
數據;type也必須是JFCSPopularCitiesType
,分別爲JFCSPopularCitiesTypeProvince
(省級)、JFCSPopularCitiesTypeCity
(市級)和JFCSPopularCitiesTypeArea
(縣級)。#Github 連接 JFCitySelector 輕量、靈活、可自定義的三級城市選擇器github