selectProvinceVC.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. //
  2. // selectProvinceVC.h.m
  3. //
  4. // Created by flyeagleTang on 14-4-3.
  5. // Copyright (c) 2014年 Reese. All rights reserved.
  6. //
  7. #import "selectProvinceVC.h"
  8. #import "JXChatViewController.h"
  9. #import "AppDelegate.h"
  10. #import "JXLabel.h"
  11. #import "JXImageView.h"
  12. //#import "JXCell.h"
  13. #import "JXRoomPool.h"
  14. #import "JXTableView.h"
  15. #import "JXNewFriendViewController.h"
  16. #import "menuImageView.h"
  17. #import "JXConstant.h"
  18. #import "selectCityVC.h"
  19. #import <CoreLocation/CoreLocation.h>
  20. #define row_height 40
  21. @interface selectProvinceVC ()<CLLocationManagerDelegate>
  22. @property(nonatomic,strong)CLLocationManager *locationManager;
  23. @property(nonatomic,strong)NSMutableString * cityName;
  24. @end
  25. @implementation selectProvinceVC
  26. @synthesize showCity;
  27. @synthesize selected;
  28. @synthesize delegate;
  29. @synthesize didSelect;
  30. @synthesize selValue;
  31. @synthesize showArea;
  32. - (id)init
  33. {
  34. self = [super init];
  35. if (self) {
  36. self.provinceId = 0;
  37. self.areaId = 0;
  38. self.cityId = 0;
  39. self.heightHeader = JX_SCREEN_TOP;
  40. self.heightFooter = 0;
  41. self.isGotoBack = YES;
  42. self.title =Localized(@"selectProvinceVC_SelProvince");
  43. //self.view.frame = CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT);
  44. [self createHeadAndFoot];
  45. self.isShowFooterPull = NO;
  46. self.isShowHeaderPull = NO;
  47. self.cityName = [[NSMutableString alloc]initWithString:Localized(@"selectProvinceVC_Locationing")];
  48. if(self.parentId<=0)
  49. self.parentId = 1;
  50. _province = [g_constant getProvince:self.parentId];
  51. _table.backgroundColor = [UIColor whiteColor];
  52. _array = [[NSMutableDictionary alloc]init];
  53. //定位方法
  54. if ([g_config.isOpenPositionService intValue] == 0) {
  55. [self locate];
  56. }
  57. [_array setObject:Localized(@"selectProvinceVC_Beijing") forKey:@"110100"];//110000,110100
  58. [_array setObject:Localized(@"selectProvinceVC_Shanghai") forKey:@"310100"];//310000,310100
  59. [_array setObject:Localized(@"selectProvinceVC_Guangzhou") forKey:@"440100"];//440000,440100
  60. [_array setObject:Localized(@"selectProvinceVC_Shenzhen") forKey:@"440300"];//440000,440300
  61. }
  62. return self;
  63. }
  64. -(void)dealloc{
  65. self.parentName = nil;
  66. self.selValue = nil;
  67. // [_province release];
  68. [_array removeAllObjects];
  69. // [_array release];
  70. // [super dealloc];
  71. }
  72. - (void)viewDidLoad
  73. {
  74. [super viewDidLoad];
  75. }
  76. - (void)viewWillAppear:(BOOL)animated
  77. {
  78. [super viewWillAppear:animated];
  79. }
  80. - (void)didReceiveMemoryWarning
  81. {
  82. [super didReceiveMemoryWarning];
  83. // Dispose of any resources that can be recreated.
  84. }
  85. #pragma mark ---------tableView协议----------------
  86. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  87. {
  88. return 3;
  89. }
  90. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  91. UIView* v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, row_height)];
  92. v.backgroundColor = HEXCOLOR(0xF2F2F2);
  93. UILabel* p = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 300, row_height)];
  94. NSString* s;
  95. switch (section) {
  96. case 0:
  97. s = Localized(@"selectProvinceVC_NowLocation");
  98. break;
  99. case 1:
  100. s = Localized(@"selectProvinceVC_HotCity");
  101. break;
  102. case 2:
  103. s = Localized(@"selectProvinceVC_SelCity");
  104. break;
  105. }
  106. p.text = s;
  107. p.font = g_factory.font15;
  108. p.textColor = HEXCOLOR(0x999999);
  109. [v addSubview:p];
  110. [_table addToPool:v];
  111. return v;
  112. }
  113. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  114. {
  115. NSInteger n;
  116. switch (section) {
  117. case 0:
  118. n = 1;
  119. break;
  120. case 1:
  121. n = [[_array allKeys] count];
  122. break;
  123. case 2:
  124. n = [[_province allKeys] count];
  125. break;
  126. }
  127. return n;
  128. }
  129. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  130. {
  131. UITableViewCell *cell=nil;
  132. NSString* cellName = [NSString stringWithFormat:@"msg_%d_%ld_%ld",_refreshCount,indexPath.row,(long)indexPath.section];
  133. cell = [tableView dequeueReusableCellWithIdentifier:cellName];
  134. // if(cell==nil){
  135. cell = [UITableViewCell alloc];
  136. [_table addToPool:cell];
  137. cell = [cell initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
  138. cell.selectionStyle = UITableViewCellSelectionStyleGray;
  139. UILabel* p = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 300, row_height)];
  140. NSMutableString* s;
  141. switch (indexPath.section) {
  142. case 0:
  143. s = self.cityName;
  144. break;
  145. case 1:
  146. s = [[_array allValues] objectAtIndex:indexPath.row];
  147. break;
  148. case 2:
  149. s = [g_constant.province_name objectAtIndex:indexPath.row];
  150. break;
  151. }
  152. p.text = s;
  153. p.font = g_factory.font16;
  154. [cell addSubview:p];
  155. // [p release];
  156. if([self tableView:_table numberOfRowsInSection:indexPath.section] != indexPath.row+1){
  157. UIView* line = [[UIView alloc] initWithFrame:CGRectMake(18,row_height-1,JX_SCREEN_WIDTH-18-20,LINE_WH)];
  158. line.backgroundColor = THE_LINE_COLOR;
  159. [cell addSubview:line];
  160. // [line release];
  161. }
  162. if(indexPath.section==2){
  163. UIImageView* iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"more_flag"]];
  164. iv.frame = CGRectMake(JX_SCREEN_WIDTH-20, (row_height-13)/2, 7, 13);
  165. [cell addSubview:iv];
  166. // [iv release];
  167. }
  168. // }
  169. // //清除cell上所有空间
  170. // NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
  171. // for (UIView *subview in subviews) {
  172. // [subview removeFromSuperview];
  173. // }
  174. // [subviews release];
  175. //
  176. //
  177. //
  178. // //重新添加定位后的Label
  179. // UILabel* p = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 300, row_height)];
  180. //
  181. // p.text = self.cityName;
  182. // p.font = g_factory.font14;
  183. //
  184. // [cell addSubview:p];
  185. // [p release];
  186. //
  187. return cell;
  188. }
  189. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  190. { [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  191. if(indexPath.section==0){
  192. self.selValue = self.cityName;
  193. // NSLog(@"%@",g_constant.cityN);
  194. //市
  195. NSString * cityNameS = [NSString stringWithFormat:@"%@%@",self.cityName,Localized(@"selectProvinceVC_City")];
  196. //县
  197. NSString * cityNameX = [NSString stringWithFormat:@"%@%@",self.cityName,Localized(@"selectProvinceVC_County")];
  198. //区
  199. NSString * cityNameQ = [NSString stringWithFormat:@"%@%@",self.cityName,Localized(@"selectProvinceVC_Area")];
  200. if ([g_constant.cityN valueForKey:self.cityName]) {
  201. self.cityId = [[g_constant.cityN valueForKey:self.cityName] intValue];
  202. self.provinceId = [[g_constant.cityN valueForKey:self.cityName] intValue];
  203. }else if ([g_constant.cityN valueForKey:cityNameS]){
  204. self.cityId = [[g_constant.cityN valueForKey:cityNameS] intValue];
  205. self.provinceId = [[g_constant.cityN valueForKey:cityNameS] intValue];
  206. }else if ([g_constant.cityN valueForKey:cityNameX]){
  207. self.cityId = [[g_constant.cityN valueForKey:cityNameX] intValue];
  208. self.provinceId = [[g_constant.cityN valueForKey:cityNameX] intValue];
  209. }else if ([g_constant.cityN valueForKey:cityNameQ]){
  210. self.cityId = [[g_constant.cityN valueForKey:cityNameQ] intValue];
  211. self.provinceId = [[g_constant.cityN valueForKey:cityNameQ] intValue];
  212. }
  213. else{
  214. if ([self.cityName isEqualToString:Localized(@"selectProvinceVC_Locationing")]|| [self.cityName isEqualToString:@""]) {
  215. self.selValue = Localized(@"selectProvinceVC_LocationFiled");
  216. }
  217. self.cityId = 1;
  218. self.provinceId = 1;
  219. }
  220. // self.provinceId = 1;
  221. }
  222. if(indexPath.section==1){
  223. self.selValue = [[_array allValues] objectAtIndex:indexPath.row];
  224. self.selected = [[[_array allKeys] objectAtIndex:indexPath.row] intValue];
  225. self.provinceId = [[g_constant getParentWithCityId:self.selected] intValue];
  226. self.cityId = self.selected;
  227. }
  228. if(indexPath.section==2){
  229. self.selValue = [g_constant.province_name objectAtIndex:indexPath.row];
  230. self.selected = [[g_constant.province_value objectAtIndex:indexPath.row] intValue];
  231. self.provinceId = self.selected;
  232. if(self.showCity){
  233. selectCityVC* vc = [selectCityVC alloc];
  234. vc.parentName = self.selValue;
  235. vc.parentId = self.selected;
  236. vc.didSelect = @selector(doSelect:);
  237. vc.delegate = self;
  238. vc.showArea = self.showArea;
  239. vc = [vc init];
  240. // [g_window addSubview:vc.view];
  241. [g_navigation pushViewController:vc animated:YES];
  242. return;
  243. }
  244. }
  245. if (delegate && [delegate respondsToSelector:didSelect])
  246. // [delegate performSelector:didSelect withObject:self];
  247. [delegate performSelectorOnMainThread:didSelect withObject:self waitUntilDone:NO];
  248. [self actionQuit];
  249. }
  250. //-(NSInteger)getCityIdForCity:(NSString*)cityName{
  251. // int n = 0;
  252. //
  253. // for (id key in [g_constant.city allValues]) {
  254. //
  255. // if ([cityName isEqualToString:value]) {
  256. //
  257. // }
  258. // }
  259. //
  260. //
  261. //
  262. // return 1;
  263. //}
  264. -(void)doSelect:(selectCityVC*)sender{
  265. [self actionQuit];
  266. if(sender.selected==sender.parentId)
  267. self.selValue = sender.selValue;
  268. else
  269. self.selValue = [NSString stringWithFormat:@"%@-%@",self.selValue,sender.selValue];
  270. self.provinceId = sender.parentId;
  271. self.cityId = sender.cityId;
  272. self.areaId = sender.areaId;
  273. self.selected = sender.selected;
  274. if (delegate && [delegate respondsToSelector:didSelect])
  275. // [delegate performSelector:didSelect withObject:self];
  276. [delegate performSelectorOnMainThread:didSelect withObject:self waitUntilDone:NO];
  277. }
  278. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  279. {
  280. return row_height;
  281. }
  282. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  283. return row_height;
  284. }
  285. #pragma mark - 定位方法
  286. - (void)locate
  287. {
  288. // 判断定位操作是否被允许
  289. if([CLLocationManager locationServicesEnabled]) {
  290. self.locationManager = [[CLLocationManager alloc] init] ;
  291. self.locationManager.delegate = self;
  292. }else {
  293. //提示用户无法进行定位操作
  294. UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:
  295. Localized(@"JX_Tip") message:Localized(@"JXServer_CannotLocation") delegate:nil cancelButtonTitle:Localized(@"JX_Cencal") otherButtonTitles:Localized(@"JX_Confirm"), nil];
  296. [alertView show];
  297. }
  298. // 开始定位
  299. [self.locationManager startUpdatingLocation];
  300. [self.locationManager stopUpdatingHeading];
  301. }
  302. //实现定位协议回调方法
  303. #pragma mark - CoreLocation Delegate
  304. -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
  305. {
  306. //此处locations存储了持续更新的位置坐标值,取最后一个值为最新位置,如果不想让其持续更新位置,则在此方法中获取到一个值之后让locationManager stopUpdatingLocation
  307. CLLocation *currentLocation = [locations lastObject];
  308. // 获取当前所在的城市名
  309. CLGeocoder *geocoder = [[CLGeocoder alloc] init];
  310. //根据经纬度反向地理编译出地址信息
  311. [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *array, NSError *error)
  312. {
  313. if (array.count > 0)
  314. {
  315. CLPlacemark *placemark = [array objectAtIndex:0];
  316. //将获得的所有信息显示到label上
  317. // NSLog(@"%@",placemark.name);
  318. //获取城市
  319. NSString *city = placemark.locality;
  320. if (!city) {
  321. //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
  322. city = placemark.administrativeArea;
  323. }
  324. if (city) {
  325. self.cityName = [NSMutableString stringWithString:city];
  326. }
  327. [_table reloadData];
  328. }
  329. else if (error == nil && [array count] == 0)
  330. {
  331. // NSLog(@"No results were returned.");
  332. }
  333. else if (error != nil)
  334. {
  335. // NSLog(@"An error occurred = %@", error);
  336. }
  337. }];
  338. //系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新
  339. [manager stopUpdatingLocation];
  340. }
  341. //
  342. //- (void)locationManager:(CLLocationManager *)manager
  343. //
  344. // didFailWithError:(NSError *)error {
  345. //
  346. // if (error.code == kCLErrorDenied) {
  347. //
  348. // // 提示用户出错原因,可按住Option键点击 KCLErrorDenied的查看更多出错信息,可打印error.code值查找原因所在
  349. //
  350. // }
  351. //
  352. //}
  353. @end