- (void)viewDidLoad {
[superviewDidLoad];
[self_loadData];
[self_initSubView];
}
#pragma mark - 加载数据
- (void)_loadData{
//1 读取数据
NSString * path=[[NSBundlemainBundle]pathForResource:@"cities.plist"ofType:nil];
NSDictionary * dic=[NSDictionarydictionaryWithContentsOfFile:path];
//2 封装省份和首字母
NSArray * prov=dic[@"provinces"];
for(NSString * strin prov){
JRProvince * pro=[JRProvincegetProWithName:str];
[self.provinceaddObject:pro];
}
//3 封装地市
self.cities=dic[@"cities"];
}
- (void)_initSubView{
UITableView * tableView=[[UITableViewalloc]initWithFrame:CGRectMake(0, 20,self.view.frame.size.width,self.view.frame.size.height-20)style:UITableViewStylePlain];
tableView.delegate=self;
tableView.dataSource=self;
tableView.sectionFooterHeight=0;//设置尾部高度为0
self.tableView=tableView;
[self.viewaddSubview:tableView];
}
#pragma mark - UITableViewDataSource
//返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//由省份计算每个省份的地市个数
JRProvince * pro=self.province[section];
NSArray * temArray=self.cities[pro.name];
return temArray.count;
}
//返回分组的数量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
returnself.province.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * identy=@"myTable";
UITableViewCell * cell=[tableViewdequeueReusableCellWithIdentifier:identy ];
if(cell==nil){
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identy];
cell.selectionStyle=UITableViewCellSelectionStyleDefault;
}
//根据省份获取城市列表,并且根据索引返回对应城市
JRProvince * pro=self.province[indexPath.section];
NSArray * temArray=self.cities[pro.name];
cell.textLabel.text=temArray[indexPath.row];
return cell;
}
//返回分组标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
JRProvince * pro=self.province[section];
return pro.name;
}
//返回分组索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
//循环遍历省份的数组,返回对应首字母作为索引
NSMutableArray * indexArray=[NSMutableArrayarray];
for(JRProvince * proin self.province){
//防止重复添加
if(![indexArraycontainsObject:[NSStringstringWithFormat:@"%c",pro.pinyin]]){
[indexArray addObject:[NSStringstringWithFormat:@"%c",pro.pinyin]];
}
}
return indexArray;
}
#pragma mark * 由于重复未添加的索引指向--第一次添加的首字母
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
int tem=0;
for(JRProvince * proin self.province){
if(pro.pinyin==[titlecharacterAtIndex:0]){
break;
}
tem++;
}
return tem;
}
#pragma mark * 选中cell,跳转到cell组名在最顶端
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSIndexPath * index=[NSIndexPathindexPathForRow:0 inSection:indexPath.section];
[self.tableViewscrollToRowAtIndexPath:index atScrollPosition:UITableViewScrollPositionTopanimated:YES];
}
//返回城市列表的首字母
+ (JRProvince * )getProWithName:(NSString * )name{
JRProvince * pro=[[JRProvincealloc]init];
pro.name=name;
//由于该工具类不能识别多音字,因此对重庆特殊处理
if([name isEqualToString:@"重庆"]){
pro.pinyin='C';
}else{
pro.pinyin=pinyinFirstLetter([namecharacterAtIndex:0])-32;
}
return pro;
}