利用tableView分组实现省份各个城市的分组,利用首字母作为索引

- (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;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值