iOS开发中我们项目中常常会使用地区列表,筛选,定位等功能,此处单单选出主要内容展示,另附有免积分demo,欢迎指正和交流
效果如下:
//遵守的代理CLLocationManagerDelegate,UIAlertViewDelegate,UITableViewDelegate,UITableViewDataSource
- (void)viewDidLoad {
[superviewDidLoad];
self.flageArray=[NSMutableArrayarray];
_cityArray=[NSMutableArrayarray];
_indexArray=[NSMutableArrayarray];
_resultArray=[NSMutableArrayarray];
//根据接口中的标示将接口中杂乱的地区重新排序添加在数组中
for (int i='A'; i<='Z'; i++) {
NSMutableArray * array=[NSMutableArrayarray];
BOOL isShow=YES;
for (int j=0; j<self.listEntitys.count; j++) {
LocationCityEntity * entity=[self.listEntitysobjectAtIndex:j];
int aa=[entity.zxlettercharacterAtIndex:0];
if (aa==i) {
[array addObject:entity];
if (isShow) {
char bb=i;
[self.indexArrayaddObject:[NSStringstringWithFormat:@"%c",bb]];
isShow=NO;
}
}else
{
}
}
//将排序后的数组放进地区数组中
if (array.count>0) {
[self.cityArrayaddObject:array];
}
}
//系统的搜索
_searchBar=[[UISearchBaralloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,44)];
_searchBar.placeholder=@"请输入城市中文名称";
self.myTableView.tableHeaderView=_searchBar;
_seachDisplay=[[UISearchDisplayControlleralloc]initWithSearchBar:_searchBarcontentsController:self];
_seachDisplay.searchResultsDelegate=self;
_seachDisplay.searchResultsDataSource=self;
self.myTableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
self.myTableView.sectionIndexBackgroundColor=[UIColorclearColor];
}
- (void)viewWillAppear:(BOOL)animated
{
[superviewWillAppear:animated];
// self.tabBarController.tabBar.hidden=YES;
self.isShow=YES;
self.view.backgroundColor=[UIColorwhiteColor];
self.extendedLayoutIncludesOpaqueBars=NO;
// [MobClick beginLogPageView:@"城市切换"];//("PageOne"为页面名称,可自定义)
}
- (void)viewWillDisappear:(BOOL)animated
{
[superviewWillDisappear:animated];
// [MobClick endLogPageView:@"城市切换"];
}
//定位使用的
-(CLLocationManager *)locationManager{
if (!_locationManager) {
_locationManager = [[CLLocationManageralloc]init];
_locationManager.delegate =self;
_locationManager.desiredAccuracy =kCLLocationAccuracyBest;
_locationManager.distanceFilter =10.0f;
if([_locationManagerrespondsToSelector:@selector(requestWhenInUseAuthorization)])
{
// [_locationManager requestAlwaysAuthorization]; //永久授权
// [_locationManager requestWhenInUseAuthorization]; //使用中授权
}
}
return_locationManager;
}
//定位的协议方法
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
switch (status) {
casekCLAuthorizationStatusNotDetermined:
if ([managerrespondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[managerrequestWhenInUseAuthorization];
}
break;
casekCLAuthorizationStatusDenied:
locationSuccess =NO;
cityString =@"定位失败";
[self.myTableViewreloadSections:[NSIndexSetindexSetWithIndex:0]withRowAnimation:UITableViewRowAnimationNone];
break;
default:
break;
}
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
[self.locationManagerstopUpdatingLocation];
CLLocation *currentLocation = [locationslastObject];
CLGeocoder *geocoder = [[CLGeocoderalloc]init];
__weaktypeof(self) weakSelf =self;
[geocoder reverseGeocodeLocation:currentLocationcompletionHandler:^(NSArray *placemarks,NSError *error) {
if (placemarks.count>0&&!error) {
CLPlacemark *placemark = placemarks[0];
// DLog(@"%@,%@",placemark.locality,[self existCity:placemark])
cityString = [selfexistCity:placemark];
locationSuccess = [cityStringlength];
if (!locationSuccess) {
cityString = [NSStringstringWithFormat:@"暂不支持%@",[placemark.localitysubstringToIndex:placemark.locality.length-1]];
}
[weakSelf.myTableViewreloadSections:[NSIndexSetindexSetWithIndex:0]withRowAnimation:UITableViewRowAnimationNone];
}else{
locationSuccess =NO;
cityString =@"定位失败";
[weakSelf.myTableViewreloadSections:[NSIndexSetindexSetWithIndex:0]withRowAnimation:UITableViewRowAnimationNone];
}
}];
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
[self.locationManagerstopUpdatingLocation];
// [SVProgressHUD showErrorWithStatus:error.localizedDescription];
locationSuccess =NO;
cityString =@"定位失败";
//刷新0 组的方法
[self.myTableViewreloadSections:[NSIndexSetindexSetWithIndex:0]withRowAnimation:UITableViewRowAnimationNone];
}
//转换城市使用的方法
-(NSString *)existCity:(CLPlacemark *)city{
for (int i =0; i<self.listEntitys.count; i++) {
LocationCityEntity *entity = [self.listEntitysobjectAtIndex:i];
if ([city.localityhasPrefix:entity.zxprovince]) {
// [GlobalData setLocationCity:entity.zxprovince];
return entity.zxprovince;
}elseif ([city.addressDictionary[@"State"]hasPrefix:entity.zxprovince])
{
// [GlobalData setLocationCity:[NSString stringWithFormat:@"%@,%@",entity.zxprovince,city.locality]];
return [NSStringstringWithFormat:@"%@,%@",entity.zxprovince,city.locality];
}
}
return [NSStringstringWithFormat:@"暂时没有开通%@站点,请选择其他城市",city];
}
//传值的效果
-(void)pushCityEntiyArr:(NSMutableArray *)citiyEntiyArr cityStr:(NSString *)cityStr viewType:(NSString *)viewType
{
_listEntitys = [NSMutableArrayarray];
_listEntitys = citiyEntiyArr;
cityString = cityStr;
_viewType = viewType;
if ([cityStrisEqualToString:@"定位失败"]||[cityStrisEqualToString:@""])
{
[self.locationManagerstartUpdatingLocation];
}
[self.myTableViewreloadData];
}
//返回组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView!=self.myTableView)
{
return1;
}else
{
returnself.cityArray.count+1+1;
}
}
//返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView!=self.myTableView)
{
//返回搜索结果的值
//现将之前搜索的内容移除
[_resultArrayremoveAllObjects];
for (NSArray * subArrayinself.cityArray)
{
for (LocationCityEntity * entity in subArray)
{
NSRange range=[entity.zxprovincerangeOfString:_searchBar.text];
if (range.location!=NSNotFound)
{
[_resultArrayaddObject:entity];
}
}
}
return_resultArray.count;
}else
{
if(section ==0)
{
return1;
}elseif(section ==1)
{
return1;
}else
{
return [[self.cityArrayobjectAtIndex:section-2]count];
}
}
}
//返回行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section==1) {
returnceilf(((float)[GlobalDatahotCityArr].count/3))*40.0;
}else
{
return50.0;
}
}
//头视图高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return40*SCREEN_WIDTH/414;
}
//组头显示的内容
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
if (tableView==self.myTableView) {
UIView *view = [[UIViewalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,40*SCREEN_WIDTH/414)];
[view setBackgroundColor:[UIColorcolorWithRed:233/255.fgreen:234/255.fblue:235/255.falpha:1]];
UILabel *titleLabel =[[UILabelalloc]initWithFrame:CGRectMake(15,5,200,21)];
if (section ==0) {
titleLabel.text =@"当前定位";
}elseif (section==1)
{
titleLabel.text=@"热门城市";
}else
{
//城市列表
titleLabel.text = [self.indexArrayobjectAtIndex:section-2];
}
[view addSubview:titleLabel];
return view;
}else
{
UIView *view = [[UIViewalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,40*SCREEN_WIDTH/414)];
[view setBackgroundColor:[UIColorcolorWithRed:233/255.fgreen:234/255.fblue:235/255.falpha:1]];
UILabel *titleLabel =[[UILabelalloc]initWithFrame:CGRectMake(15,5,200,21)];
titleLabel.text=@"搜索结果";
[view addSubview:titleLabel];
return view;
}
}
//cell的显示内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (tableView!=self.myTableView) {
staticNSString* cellId=@"cellId";
if (cell==nil) {
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellId];
}
cell.textLabel.text=[[_resultArrayobjectAtIndex:indexPath.row]zxprovince];
}else
{
if (indexPath.section ==0) {
diweiCell *myCell = [self.myTableViewdequeueReusableCellWithIdentifier:@"diweiCell"forIndexPath:indexPath];
myCell.locationLabel.text = [GlobalDatalocationCity];
cell = myCell;
}elseif (indexPath.section ==1) {
hotCityCell *myCell = [self.myTableViewdequeueReusableCellWithIdentifier:@"hotCityCell"forIndexPath:indexPath];
[myCell setMycollectionView];
myCell.hotArray=[GlobalDatahotCityArr];
[myCell.MyCollectionViewreloadData];
__weaktypeof(self) weakSelf=self;
myCell.didCollection=^(LocationCityEntity * entity,NSIndexPath *indexpa)
{
typeof(self) strongSelf=weakSelf;
NSString *msg = [NSStringstringWithFormat:@"切换地区将会登出当前账号,是否要切换至%@站?",entity.zxprovince];
UIAlertView *alert =[[UIAlertViewalloc]initWithTitle:@"提示"message:msgdelegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];
alert.tag =indexpa.row;
strongSelf.isShow=NO;
[alert show];
};
cell = myCell;
}else
{
cityCell *myCell = [self.myTableViewdequeueReusableCellWithIdentifier:@"cityCell"forIndexPath:indexPath];
if (self.cityArray.count>0) {
LocationCityEntity *entity = [[self.cityArrayobjectAtIndex:indexPath.section-2]objectAtIndex:indexPath.row];
myCell.cityLabel.text = entity.zxprovince;
[self.flageArrayaddObject:entity.zxletter];
}
cell = myCell;
}
}
return cell;
}
//cell的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
tableView.tag=1114;
if (tableView!=self.myTableView)
{
if (self.resultArray.count>0)
{
NSString *city = [[self.resultArrayobjectAtIndex:indexPath.row]zxprovince];
LocationCityEntity * entity;
for (int i=0; i<self.listEntitys.count; i++) {
LocationCityEntity * entity1 =[self.listEntitysobjectAtIndex:i];
if ([entity1.zxprovinceisEqualToString:city]) {
entity=entity1;
}else
{
}
}
//切换地区操作
NSString *msg = [NSStringstringWithFormat:@"切换地区将会登出当前账号,是否要切换至%@站?",city];
UIAlertView *alert =[[UIAlertViewalloc]initWithTitle:@"提示"message:msgdelegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];
alert.tag = indexPath.row;
alert.message=msg;
[alert show];
}
}else{
if(indexPath.section >1)
{
if (self.cityArray.count>0)
{
LocationCityEntity *entity = [[self.cityArrayobjectAtIndex:indexPath.section-2]objectAtIndex:indexPath.row];
NSString *msg = [NSStringstringWithFormat:@"切换地区将会登出当前账号,是否要切换至%@站?",entity.zxprovince];
UIAlertView *alert =[[UIAlertViewalloc]initWithTitle:@"提示"message:msgdelegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"确定",nil];
alert.tag = indexPath.row;
alert.message=msg;
self.indexTag = (int)indexPath.section-2;
[alert show];
}
}
}
}
#pragma mark ----小标志代码
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
if (tableView==self.myTableView)
{
NSMutableArray * array=[NSMutableArrayarrayWithArray:self.indexArray ];
//[array insertObject:@"" atIndex:0];
[array insertObject:@""atIndex:0];
return array;
}else
{
returnnil;
}
}
//弹出信息之后的操作
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
UITableView * _tableView=(UITableView*)[self.viewviewWithTag:1114];
if (_tableView==self.myTableView) {
if (buttonIndex ==1) {
}
}elseif(self.isShow==NO)
{
if (buttonIndex ==1) {
}
}else
{
if (buttonIndex ==1) {
}
}
}
附件:地区筛选的demo(免积分下载),使用了block ,类扩展,本地保存等(只为共同学习)
http://download.csdn.net/detail/bddzzw/9597894