areaView.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //
  2. // areaView.m
  3. // BuguLive
  4. //
  5. // Created by fanwe2014 on 16/7/19.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import "areaView.h"
  9. #import "areaModel.h"
  10. #define CANCLE_LABEL_TAG 11
  11. #define CONFRM_LABEL_TAG 12
  12. @interface areaView ()<UIPickerViewDataSource,UIPickerViewDelegate>
  13. {
  14. UIPickerView *pickView;
  15. NetHttpsManager *_httpManager;
  16. NSString *_provinceId;
  17. NSString *_provinceString;//省份
  18. NSString *_cityString;//城市
  19. NSArray *_placeArray;
  20. GlobalVariables *_BuguLive;
  21. int _provinceCount;//省份的下标以备好加载传过来的省份
  22. int _cityCount;//城市的下标以备好加载传过来的城市
  23. }
  24. @property (nonatomic, assign) NSInteger firstCurrentIndex;//第一行当前位置
  25. @property (nonatomic, assign) NSInteger secondCurrentIndex;//第二行当前位置
  26. @end
  27. @implementation areaView
  28. - (id)initWithDelegate:(id<AreaDelegate>)delegate withCity:(NSString *)city
  29. {
  30. if (self = [super init])
  31. {
  32. _provinceCount = 0;
  33. _cityString = 0;
  34. _BuguLive = [GlobalVariables sharedInstance];
  35. _placeArray = [city componentsSeparatedByString:@" "];
  36. if (_placeArray.count > 1)
  37. {
  38. _provinceString = [_placeArray firstObject];
  39. _cityString = [_placeArray lastObject];
  40. }
  41. _httpManager = [NetHttpsManager manager];
  42. self.proniceArray = [[NSMutableArray alloc]init];
  43. self.cityArray = [[NSMutableArray alloc]init];
  44. self.bigArray = [[NSMutableArray alloc]init];
  45. self.backgroundColor = kBackGroundColor;
  46. self.delegate = delegate;
  47. //存版本
  48. NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
  49. NSString *filePath = [cachePath stringByAppendingPathComponent:@"versions.plist"];
  50. NSMutableDictionary *dict1 = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
  51. NSString *versions = [dict1 objectForKey:@"versions"];
  52. if ([versions isEqualToString:_BuguLive.appModel.region_versions])
  53. {
  54. //获取Documents目录
  55. NSString *docPath2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
  56. //还要指定存储文件的文件名称,仍然使用字符串拼接
  57. NSString *filePath2 = [docPath2 stringByAppendingPathComponent:@"Province.plist"];
  58. self.allArray = [NSMutableArray arrayWithContentsOfFile:filePath2];
  59. if (self.allArray && [self.allArray isKindOfClass:[NSMutableArray class]])
  60. {
  61. [self setRegionList:self.allArray];
  62. }else
  63. {
  64. self.allArray = [[NSMutableArray alloc]init];
  65. [self loadNet];
  66. }
  67. }else
  68. {
  69. self.allArray = [[NSMutableArray alloc]init];
  70. [self loadNet];
  71. }
  72. }
  73. return self;
  74. }
  75. //取数据
  76. - (void)setRegionList:(NSMutableArray *)allDataArray{
  77. for (NSDictionary *dict in allDataArray)
  78. {
  79. areaModel *model = [[areaModel alloc]init];
  80. if ([[dict toString:@"region_level"] isEqualToString:@"2"])
  81. {
  82. model.id = [dict toString:@"id"];
  83. model.pid= [dict toString:@"pid"];
  84. model.name = [dict toString:@"name"];
  85. model.region_level = [dict toString:@"region_level"];
  86. model.modelArray = [[NSMutableArray alloc]init];
  87. for (NSDictionary *dict1 in allDataArray)
  88. {
  89. if ([model.id isEqualToString:[dict1 toString:@"pid"]])
  90. {
  91. [model.modelArray addObject:[dict1 toString:@"name"]];//存城市
  92. }
  93. }
  94. [_bigArray addObject:model];//大数组存储省份的数据和省份对应的城市
  95. }
  96. }
  97. [self laodNowLocationData];//加载传过来的省份和城市
  98. [self createView];
  99. }
  100. //获取当前省份和城市的下标
  101. -( void)laodNowLocationData
  102. {
  103. for (int i = 0; i < _bigArray.count ; i ++)
  104. {
  105. areaModel *model = _bigArray[i];
  106. // model.modelArray = [[NSMutableArray alloc]init];
  107. if ([model.name isEqualToString:_provinceString])
  108. {
  109. _provinceCount = i;
  110. for (int j = 0; j < model.modelArray.count; j ++)
  111. {
  112. if ([model.modelArray[j] isEqualToString:_cityString])
  113. {
  114. _cityCount = j;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. //加载数据
  121. - (void)loadNet
  122. {
  123. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  124. [parmDict setObject:@"user_center" forKey:@"ctl"];
  125. [parmDict setObject:@"region_list" forKey:@"act"];
  126. [_httpManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  127. {
  128. if ([responseJson toInt:@"status"] == 1)
  129. {
  130. //存版本
  131. NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
  132. NSString *filePath = [cachePath stringByAppendingPathComponent:@"versions.plist"];
  133. NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];
  134. [dict1 setObject:[responseJson toString:@"region_versions"] forKey:@"versions"];
  135. [dict1 writeToFile:filePath atomically:YES];
  136. NSArray *areaArray = [responseJson objectForKey:@"region_list"];
  137. if (areaArray)
  138. {
  139. if (areaArray.count > 0 && [areaArray isKindOfClass:[NSArray class]])
  140. {
  141. //获取Documents目录
  142. NSString *docPath2 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
  143. //还要指定存储文件的文件名称,仍然使用字符串拼接
  144. NSString *filePath2 = [docPath2 stringByAppendingPathComponent:@"Province.plist"];
  145. NSLog(@"filePath2==%@",filePath2);
  146. [areaArray writeToFile:filePath2 atomically:YES];
  147. self.allArray = [NSMutableArray arrayWithArray:areaArray];
  148. [self setRegionList:self.allArray];
  149. }
  150. }
  151. }
  152. } FailureBlock:^(NSError *error)
  153. {
  154. }];
  155. }
  156. //创建UI控件
  157. - (void)createView
  158. {
  159. UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 40)];
  160. view.backgroundColor = kBackGroundColor;
  161. [self addSubview:view];
  162. UILabel *cancle = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 60, 40)];
  163. cancle.userInteractionEnabled = YES;
  164. cancle.text = ASLocalizedString(@"取消");
  165. cancle.tag = CANCLE_LABEL_TAG;
  166. cancle.textColor = kAppGrayColor1;
  167. cancle.textAlignment = NSTextAlignmentCenter;
  168. [self addSubview:cancle];
  169. UITapGestureRecognizer *canTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
  170. [cancle addGestureRecognizer:canTap];
  171. UILabel *confrm = [[UILabel alloc] initWithFrame:CGRectMake(kScreenW - 70, 0, 60, 40)];
  172. confrm.userInteractionEnabled = YES;
  173. confrm.text = ASLocalizedString(@"确认");
  174. confrm.tag = CONFRM_LABEL_TAG;
  175. confrm.textColor = kAppGrayColor1;
  176. confrm.textAlignment = NSTextAlignmentCenter;
  177. [self addSubview:confrm];
  178. UITapGestureRecognizer *conTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
  179. [confrm addGestureRecognizer:conTap];
  180. pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, kScreenW, 150)];
  181. pickView.backgroundColor = kWhiteColor;
  182. pickView.delegate = self;
  183. pickView.dataSource = self;
  184. _firstCurrentIndex = _provinceCount;
  185. _secondCurrentIndex = _cityCount;
  186. [pickView selectRow:_provinceCount inComponent:0 animated:YES];
  187. [pickView selectRow:_cityCount inComponent:1 animated:YES];
  188. [self addSubview:pickView];
  189. }
  190. // 一共有多少列
  191. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
  192. {
  193. return 2;
  194. }
  195. // 第component列一共有多少行
  196. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
  197. {
  198. if(component == 0)
  199. {
  200. if (_bigArray.count > 0)
  201. {
  202. return _bigArray.count;
  203. }else
  204. {
  205. return 0;
  206. }
  207. }else
  208. {
  209. if (_bigArray.count > 0 )
  210. {
  211. areaModel *model = _bigArray[_firstCurrentIndex];
  212. return model.modelArray.count;
  213. }else
  214. {
  215. return 0;
  216. }
  217. }
  218. }
  219. - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
  220. {
  221. if(component == 0)
  222. {
  223. _firstCurrentIndex = row;
  224. if (row > 0)
  225. {
  226. areaModel *model = _bigArray[row];
  227. _provinceString = model.name;
  228. _cityArray = model.modelArray;
  229. _cityString = model.modelArray[0];
  230. _secondCurrentIndex = 0;
  231. }else
  232. {
  233. _cityArray = nil;
  234. areaModel *model = _bigArray[0];
  235. _provinceString = model.name;
  236. _cityString = model.modelArray[0];
  237. _secondCurrentIndex = 0;
  238. }
  239. [pickView reloadAllComponents];
  240. }
  241. else if(component == 1)
  242. {
  243. areaModel *model = _bigArray[_firstCurrentIndex];
  244. _provinceString = model.name;
  245. _cityArray = model.modelArray;
  246. _cityString = _cityArray[row];
  247. _secondCurrentIndex = row;
  248. }
  249. }
  250. //自定义 当前列 当前行 要显示的内容
  251. - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
  252. {
  253. UILabel *myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, kScreenW/3 , 30)];
  254. myView.textAlignment = NSTextAlignmentCenter;
  255. myView.font = [UIFont systemFontOfSize:14];
  256. if(component == 0)
  257. {
  258. areaModel *model = _bigArray[row];
  259. myView.text = model.name;
  260. }
  261. else if (component == 1)
  262. {
  263. areaModel *model1 = _bigArray[_firstCurrentIndex];
  264. _cityArray = model1.modelArray;;
  265. myView.text = _cityArray[row];
  266. }
  267. return myView;
  268. }
  269. //第component列的宽度是多少
  270. - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
  271. {
  272. return kScreenW/2 ;
  273. }
  274. //第component列的行高是多少
  275. - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
  276. {
  277. return 30;
  278. }
  279. //点击事件
  280. - (void)tapClick:(UITapGestureRecognizer *)tap
  281. {
  282. if (!_provinceString && !_cityString)
  283. {
  284. if (_bigArray.count)
  285. {
  286. areaModel *model = _bigArray[0];
  287. _provinceString = model.name;
  288. NSMutableArray *firstArray = model.modelArray;
  289. _cityString = firstArray[0];
  290. }
  291. }
  292. if(self.delegate)
  293. {
  294. NSInteger tag = tap.view.tag;
  295. if([self.delegate respondsToSelector:@selector(confrmCallBack:withCity:andtagIndex:)])
  296. {
  297. [self.delegate confrmCallBack:_provinceString withCity:_cityString andtagIndex:(int)tag];
  298. }
  299. }
  300. }
  301. @end