ios 地图选择城市 切换城市,点击城市按钮设置城市

实现思路

1.获取数据

1.拖出一个UItextView在他的文本框中输入城市信息

2.拖出这个textView的输出口并获得并处理他的数据他的数据存储到NSUserDefaults中

//输出口
@property (weak, nonatomic) IBOutlet UITextView *cityListTextView;
//传入汉字字符串, 返回拼音首字母,用以首字母分类城市列表
- (NSString *)firstCharactor:(NSString *)aString
{NSMutableString *str = [NSMutableString stringWithString:aString];CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformStripDiacritics,NO);NSString *pinYin = [str capitalizedString];return [pinYin substringToIndex:1];
}
//存储数据NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];NSMutableDictionary *cityMap = (NSMutableDictionary *)[userDefault objectForKey:@"xmcityMap"];NSMutableArray *cityList = [[NSMutableArray alloc] init];if (cityMap == nil || [cityMap count] == 0) {NSArray *cityListData = [self.cityListTextView.text componentsSeparatedByString:@"\n"];cityMap = [[NSMutableDictionary alloc] initWithDictionary:@{@"A":[[NSMutableArray alloc] init],@"B":[[NSMutableArray alloc] init],@"C":[[NSMutableArray alloc] init],@"D":[[NSMutableArray alloc] init],@"E":[[NSMutableArray alloc] init],@"F":[[NSMutableArray alloc] init],@"G":[[NSMutableArray alloc] init],@"H":[[NSMutableArray alloc] init],@"J":[[NSMutableArray alloc] init],@"K":[[NSMutableArray alloc] init],@"L":[[NSMutableArray alloc] init],@"M":[[NSMutableArray alloc] init],@"N":[[NSMutableArray alloc] init],@"P":[[NSMutableArray alloc] init],@"Q":[[NSMutableArray alloc] init],@"R":[[NSMutableArray alloc] init],@"S":[[NSMutableArray alloc] init],@"T":[[NSMutableArray alloc] init],@"W":[[NSMutableArray alloc] init],@"X":[[NSMutableArray alloc] init],@"Y":[[NSMutableArray alloc] init],@"Z":[[NSMutableArray alloc] init]}];for (NSString *cityName in cityListData) {if (cityName == nil || [@"" isEqualToString:cityName]) {continue;}[cityList addObject:cityName];NSString *initials = [self firstCharactor:cityName];if ([[cityMap allKeys] containsObject:initials]) {NSMutableArray *array = (NSMutableArray *)[cityMap objectForKey:initials];[array addObject:cityName];[cityMap setValue:array forKey:initials];}else{NSMutableArray *array = [[NSMutableArray alloc] init];[array addObject:cityName];[cityMap setValue:array forKey:initials];}}//存储所有城市列表[userDefault setObject:cityList forKey:@"xmcityList"];//存储所有首字母映射的字典[userDefault setObject:cityMap forKey:@"xmcityMap"];[userDefault synchronize];}

2.通过刚刚获得的数据现在来进行选择城市的功能实现

控制器结构

代码部分

行内嵌入的表格类

#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN@interface XMSwitchCityCollectionView : UICollectionView@property (nonatomic) NSInteger indexRow;@endNS_ASSUME_NONNULL_END#import "XMSwitchCityCollectionView.h"@implementation XMSwitchCityCollectionView/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {// Drawing code
}
*/@end
#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN@interface XMSwitchCityCollectionViewCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UILabel *cityName;@endNS_ASSUME_NONNULL_END#import "XMSwitchCityCollectionViewCell.h"@implementation XMSwitchCityCollectionViewCell@end

表格行

#import <UIKit/UIKit.h>NS_ASSUME_NONNULL_BEGIN@interface SwitchCityTitleTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *titleName;@endNS_ASSUME_NONNULL_END#import "SwitchCityTitleTableViewCell.h"@implementation SwitchCityTitleTableViewCell- (void)awakeFromNib {[super awakeFromNib];// Initialization code
}- (void)setSelected:(BOOL)selected animated:(BOOL)animated {[super setSelected:selected animated:animated];// Configure the view for the selected state
}@end
#import <UIKit/UIKit.h>
#import "XMSwitchCityCollectionView.h"
NS_ASSUME_NONNULL_BEGIN@interface SwitchCityShowCityTableViewCell : UITableViewCell@property (weak, nonatomic) IBOutlet XMSwitchCityCollectionView *swtichCityCollectionView;@endNS_ASSUME_NONNULL_END#import "SwitchCityShowCityTableViewCell.h"@implementation SwitchCityShowCityTableViewCell- (void)awakeFromNib {[super awakeFromNib];// Initialization code
}- (void)setSelected:(BOOL)selected animated:(BOOL)animated {[super setSelected:selected animated:animated];// Configure the view for the selected state
}@end

主体类

#import <UIKit/UIKit.h>
#import "SwitchCityTitleTableViewCell.h"
#import "SwitchCityShowCityTableViewCell.h"
#import "XMSwitchCityCollectionView.h"
#import "XMSwitchCityCollectionViewCell.h"@interface XMSwitchCityViewController : UIViewController@property (strong,nonatomic) NSMutableDictionary *cityMap;
@property (strong,nonatomic) NSArray *cityMapKeyList;@property (weak, nonatomic) IBOutlet UISearchBar *switchCitySearchBar;
@property (strong,nonatomic) NSArray *hotCityList;@property (nonatomic) BOOL isSearch;@property (strong,nonatomic) NSArray *searchCityList;@property (weak, nonatomic) IBOutlet UITableView *switchCityTableView;@end
#import "XMSwitchCityViewController.h"@interface XMSwitchCityViewController ()<UITableViewDelegate,UICollectionViewDelegate,UISearchBarDelegate>@end@implementation XMSwitchCityViewController- (void)viewDidLoad {[super viewDidLoad];[self.switchCitySearchBar setDelegate:self];self.cityMap = [[NSUserDefaults standardUserDefaults] objectForKey:@"xmcityMap"];NSArray *allKeyArray = [self.cityMap allKeys];self.hotCityList = @[@"北京市",@"上海市",@"深圳市"];//序列化器对数组进行排序的block 返回值为排序后的数组NSArray *afterSortKeyArray = [allKeyArray sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id_Nonnull obj2) {NSComparisonResult resuest = [obj1 compare:obj2];return resuest;}];for (NSString * key in afterSortKeyArray) {NSLog(@"%@",key);}self.cityMapKeyList = afterSortKeyArray;self.switchCityTableView.separatorStyle = UITableViewCellSeparatorStyleNone;// Do any additional setup after loading the view.
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{if (self.isSearch) {return 2;}return [self.cityMap allKeys].count*2 + 4;
}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{if (indexPath.row == 0) {return 50;}else if (indexPath.row == 1) {return 80;}else if (indexPath.row == 2) {return 50;}else if (indexPath.row == 3) {return 80;}else if (indexPath.row % 2 == 0) {return 50;}else{NSInteger rowCount = self.view.frame.size.width/95;NSInteger lineCount= (ceil)([[self.cityMap objectForKey:[NSString stringWithFormat:@"%@",(NSString *)self.cityMapKeyList[(indexPath.row - 4)/2]]] count]/1.0/rowCount);return 30 + lineCount * 50 + (lineCount-1)*10;}
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{if (indexPath.row == 0) {SwitchCityTitleTableViewCell *cell =(SwitchCityTitleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"SwitchCityTitleTableViewCell" forIndexPath:indexPath];[cell.titleName setText:@"定位城市"];if (self.isSearch) {[cell.titleName setText:@"搜索结果"];}// [cell addSubview:lable];cell.layer.cornerRadius = 8;cell.selectionStyle = UITableViewCellSelectionStyleNone;return cell;}else if (indexPath.row == 1) {SwitchCityShowCityTableViewCell *cell =(SwitchCityShowCityTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"SwitchCityShowCityTableViewCell" forIndexPath:indexPath];[cell.swtichCityCollectionView setIndexRow:indexPath.row];cell.layer.cornerRadius = 8;cell.selectionStyle = UITableViewCellSelectionStyleNone;[cell.swtichCityCollectionView setFrame:CGRectMake(0, 0, 120, 80)];[cell.swtichCityCollectionView reloadData];return cell;}else if (indexPath.row == 2) {SwitchCityTitleTableViewCell *cell =(SwitchCityTitleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"SwitchCityTitleTableViewCell" forIndexPath:indexPath];[cell.titleName setText:@"热门城市"];// [cell addSubview:lable];cell.layer.cornerRadius = 8;cell.selectionStyle = UITableViewCellSelectionStyleNone;return cell;}else if (indexPath.row == 3) {SwitchCityShowCityTableViewCell *cell =(SwitchCityShowCityTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"SwitchCityShowCityTableViewCell" forIndexPath:indexPath];[cell.swtichCityCollectionView setIndexRow:indexPath.row];cell.layer.cornerRadius = 8;cell.selectionStyle = UITableViewCellSelectionStyleNone;[cell.swtichCityCollectionView setFrame:cell.contentView.frame];[cell.swtichCityCollectionView reloadData];return cell;}else if (indexPath.row % 2 == 0) {SwitchCityTitleTableViewCell *cell =(SwitchCityTitleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"SwitchCityTitleTableViewCell" forIndexPath:indexPath];[cell.titleName setText:[NSString stringWithFormat:@"%@",(NSString *)self.cityMapKeyList[(indexPath.row - 4)/2]]];cell.layer.cornerRadius = 8;cell.selectionStyle = UITableViewCellSelectionStyleNone;return cell;}else{SwitchCityShowCityTableViewCell *cell =(SwitchCityShowCityTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"SwitchCityShowCityTableViewCell" forIndexPath:indexPath];[cell.swtichCityCollectionView setIndexRow:indexPath.row];cell.layer.cornerRadius = 8;cell.selectionStyle = UITableViewCellSelectionStyleNone;[cell.swtichCityCollectionView setFrame:cell.contentView.frame];[cell.swtichCityCollectionView reloadData];return cell;}}/*
#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {// Get the new view controller using [segue destinationViewController].// Pass the selected object to the new view controller.
}
*///collectionView代理方法
#pragma mark - 代理方法 Delegate Methods
// 设置分区- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {return 1;
}
#pragma mark - UICollectionViewDataSource
// 每个分区上得元素个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {if (self.isSearch) {return self.searchCityList.count;}XMSwitchCityCollectionView *xmSwitchCityCollectionView = (XMSwitchCityCollectionView *)collectionView;if (xmSwitchCityCollectionView.indexRow == 1) {return 1;} else if (xmSwitchCityCollectionView.indexRow == 3) {return 3;} else if (xmSwitchCityCollectionView.indexRow % 2 == 1) {return [[self.cityMap objectForKey:[NSString stringWithFormat:@"%@",(NSString *)self.cityMapKeyList[(xmSwitchCityCollectionView.indexRow - 4)/2]]] count];}return 0;
}#pragma mark - UICollectionViewDelegateFlowLayout
// 设置cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {XMSwitchCityCollectionViewCell *cell =(XMSwitchCityCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"XMSwitchCityCollectionViewCell" forIndexPath:indexPath];if (self.isSearch) {[cell.cityName setText:self.searchCityList[0]];return cell;}XMSwitchCityCollectionView *xmSwitchCityCollectionView = (XMSwitchCityCollectionView *)collectionView;if (xmSwitchCityCollectionView.indexRow == 1) {NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];[cell.cityName setText:[userDefaults objectForKey:@"locationAddress"]];} else if (xmSwitchCityCollectionView.indexRow == 3) {[cell.cityName setText:self.hotCityList[indexPath.row]];} else if (xmSwitchCityCollectionView.indexRow % 2 == 1) {[cell.cityName setText:((NSMutableArray *)[self.cityMap objectForKey: [NSString stringWithFormat:@"%@",(NSString *)self.cityMapKeyList[(xmSwitchCityCollectionView.indexRow - 4)/2]]])[indexPath.row]];}return cell;
}// 设置cell大小 itemSize:可以给每一个cell指定不同的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {return CGSizeMake(90, 50);
}//点击市名存储选择的市
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{XMSwitchCityCollectionViewCell *xmSwitchCityCollectionViewCell = (XMSwitchCityCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];[userDefaults setObject:xmSwitchCityCollectionViewCell.cityName.text forKey:@"currentAddress"];//跳转回需要设置城市的页面用"currentAddress"进行设置,我这里注释了,正常运行   给viewController设置id就能跳转了 /*UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];UIViewController *vc = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:@"IndexPageVc"]; [self.view.window setRootViewController:vc];*/}//搜索代理方法- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{return YES;
}// 当搜索内容变化时,执行该方法。很有用,可以实现时实搜索
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{self.isSearch = true;if (self.isSearch) {NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];NSMutableArray *cityList = (NSMutableArray *)[userDefaults objectForKey:@"xmcityList"];if (searchText == nil || [@""isEqualToString:searchText]) {self.isSearch = NO;} else if ([cityList containsObject:searchText]) {self.searchCityList = @[searchText];} else {self.searchCityList = [[NSArray alloc] init];}[self.switchCityTableView reloadData];}
}
// 键盘中,搜索按钮被按下,执行的方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{}@end

城市列表

>>城市列表

ios Objective-c 地图选择城市相关推荐

  1. android 仿饿了么地图,iOS Andriod百度地图仿百度外卖 饿了么 选择我的地址 POI检索...

    title: iOS Andriod百度地图仿百度外卖 饿了么 选择我的地址 POI检索 date: 2015-09-19 21:06:26 tags: 百度外卖选择送货地址:饿了么选择送货地址: 第 ...

  2. 移动端vue+vant+高德地图实现拖拽选址,周边选址,搜索选址,自动定位,选择城市功能,获取地址经纬度,详细地址

    效果图: 在public文件夹下的index.html文件中head标签下加上script标签如下: <script type="text/javascript">wi ...

  3. 转-iOS开发系列--地图与定位

    来自: http://www.cnblogs.com/kenshincui/p/4125570.html#autoid-3-4-0 概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功 ...

  4. iOS学习笔记-地图MapKit入门

    代码地址如下: http://www.demodashi.com/demo/11682.html 这篇文章还是翻译自raywenderlich,用Objective-C改写了代码.没有逐字翻译,如有错 ...

  5. iOS开发系列--地图与定位

    概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一个 ...

  6. vue中使用腾讯地图选择地址

    腾讯地图选择地址引发的一系列问题 2019/12/14更新 修复了自治区/直辖市无法选择的bug(比如上海之类的城市) 采用更加友好的方式来选择地址 新的文章链接:vue中使用腾讯地图选择地址(二) ...

  7. VUE2.0实现 高德地图 选择地点后 进行 行政区边界划分

    最终效果图: 最终效果达到选择全部就描绘当前级联选择框下面的所有行政区,第二级开始描绘上一层加当前所有行政区 预备知识: vue2.0.组件间传值.高德地图API(提前去高德地图提供的服务处申请好ke ...

  8. iOS—— 调用高德地图SDK

    iOS-- 调用高德地图SDK 我们在许多App中可以发现关于地图的调用以及定位等等功能都是什么重要的,比如滴滴打车,美团外卖等等都是十分依赖于地图的.所以我们就应该学会如和去调用地图.本周我就学习了 ...

  9. 如何实现“轻高精地图”的城市NOH?毫末自动驾驶的8大亮点

    4月19日,毫末的AI DAY,我没顾上看直播,本来打算会后只发一则"简单的小新闻",结果呢,一看顾维灏的演讲特别"硬核",很值得认真读一下.在这次发布会的前后 ...

最新文章

  1. c#使用正则表达式获取TR中的多个TD_一个 Vue 模板可以有多个根节点(Fragments)?
  2. 线程安全且高效的单例
  3. 【学习笔记】37、用正则表达式解析和提取数据
  4. java 一年 周数_在java,如何找到一年的总周数?_java_酷徒编程知识库
  5. 计算沙盒中一个目录的大小
  6. 【Openstack】实录手动部署Openstack Rocky 双节点(2)- Keystone
  7. YACC、LEX、JAVACC-------常用的编译工具
  8. Android中的WebView之loadDataWithBaseURL()与loadData()
  9. transform子元素,绝对定位失效
  10. WPE封包外挂教程(下)
  11. 6-2 顺序表基本操作 (10 分)
  12. xp系统计算机怎么连接到网络打印机,XP系统找不到网络打印机如何解决?
  13. KubernetsPod分析
  14. sklearn笔记19 随机森林和决策树的比较
  15. 时间管理——34枚金币
  16. 树莓派连接OLED屏
  17. 计算机网络面试常见知识点(含HTTPS和TLS)
  18. python日志处理(logging模块)
  19. shell小记:dirname
  20. 秒杀产品总是抢不到?抢单助手来报道

热门文章

  1. 浙江大学求是科学班计算机,浙大校友网
  2. ORA-22858: invalid alteration of datatype
  3. 海南大学电子信息--835软件工程论述题冲刺经验
  4. 【STM32 嵌入式设计】PS2索尼游戏手柄解析和代码开发
  5. 中国高校教师跌入“社会底层”?
  6. 2019篮球世界杯(北京五棵松场馆)之 万达体育 互动投篮机
  7. 中通的双刃剑:市值新高与模式硬伤
  8. 丘比特之箭python代码_qq飞车t3代码大全2016 | 手游网游页游攻略大全
  9. 关于SR3100的常见疑问
  10. 电信联通ADSL共享上网检测的原理与实现