SearchBase.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /**
  2. 貌似只需要1个数组来保存数据
  3. */
  4. #import "SearchBase.h"
  5. #import "MyDrop.h"//自己写的下拉菜单。蛋碎
  6. #import "TRButton.h"
  7. #import "SchCell.h"
  8. #import "CoachDetailVC.h"
  9. #import "HolderView.h"
  10. #import "Tools.h"
  11. #import <BaiduMapAPI_Map/BMKMapComponent.h>
  12. #import <BMKLocationkit/BMKLocationComponent.h>//引入定位功能所有的头文件(新)
  13. #import <BaiduMapAPI_Search/BMKSearchComponent.h>
  14. @interface SearchBase ()<DropDelegate,UITableViewDelegate,UITableViewDataSource,BMKLocationManagerDelegate,BMKGeoCodeSearchDelegate>
  15. {
  16. //分段选择器下方的滚动条。
  17. UIView* btnBar;
  18. //地区选择器
  19. MyDrop* menu;
  20. HolderView* holdV;
  21. NSMutableArray* btnArr;
  22. //这里保存的是myDrop下拉菜单的数据源
  23. NSMutableArray *firArr,*secArr;
  24. //所有地区名称,加code。pcode
  25. NSArray* areaArr;
  26. /**排列类型 1 价格 2 星级 3 人气
  27. */
  28. // ENUMSortType sortType;
  29. NSInteger sortType;
  30. /**
  31. 排列方式 1 星级由低到高 2 由高到低
  32. */
  33. NSInteger sort;
  34. TRButton* currentSegBtn;
  35. NSString* dqbh,*qxbh;
  36. //搜索用的方法名。
  37. NSString* method;
  38. BOOL refreshing;
  39. /**如果是通过上拉来实现。刷新。就是YES。
  40. 如果是NO。刷新需要移除所有数据
  41. */
  42. BOOL isPull;
  43. int currentPage;
  44. NSMutableArray* models;
  45. //地图定位
  46. CLLocationCoordinate2D myLocation;
  47. }
  48. /**每次修改搜索条件后。要移除所有。
  49. */
  50. /**分段选择器的按钮
  51. */
  52. @property (nonatomic, strong) NSMutableArray *btns;
  53. @property (nonatomic, assign) NSInteger pageIndex;
  54. @end
  55. @implementation SearchBase
  56. - (void)viewDidLoad
  57. {
  58. [super viewDidLoad];
  59. [self.view setBackgroundColor:[UIColor whiteColor]];
  60. self.navigationController.navigationBar.translucent = NO;
  61. [self configNavigationBar];
  62. self.pageIndex = 0;
  63. [self baseInit];
  64. }
  65. -(void)viewWillDisappear:(BOOL)animated
  66. {
  67. self.hidesBottomBarWhenPushed = NO;
  68. [super viewWillDisappear:animated];
  69. }
  70. - (void)didReceiveMemoryWarning {
  71. [super didReceiveMemoryWarning];
  72. }
  73. #pragma mark -
  74. -(void)baseInit
  75. {
  76. currentPage = 1;
  77. self.catalog = 0;
  78. models = [NSMutableArray array];
  79. NSArray* segmentArray = @[@"驾校排行榜",@"教练排行榜"];
  80. UISegmentedControl *segmentedControl =[[UISegmentedControl alloc]initWithItems:segmentArray];
  81. segmentedControl.frame = CGRectMake(0, 0, kSize.width - 120, 35);
  82. segmentedControl.selectedSegmentIndex = 0;
  83. segmentedControl.tintColor = defGreen;
  84. [segmentedControl addTarget:self action:@selector(clickToChoose:)forControlEvents:UIControlEventValueChanged];
  85. [self.navigationItem setTitleView:segmentedControl];
  86. CGFloat menuW = kSize.width/4;
  87. CGFloat menuY = 0;
  88. CGFloat menuH = 40;
  89. menu = [[MyDrop alloc] initWithFrame:CGRectMake(0, menuY, menuW, menuH)];
  90. [self.view addSubview:menu];
  91. [self loadArea];
  92. menu.firArr = firArr;
  93. menu.secArr = secArr;
  94. menu.delegate = self;
  95. //分段选择器的按钮
  96. btnArr = [NSMutableArray array];
  97. NSArray* btnTits = @[@"价格",@"星级",@"人气"];
  98. for (int i=0; i < btnTits.count;i++) {
  99. TRButton* btn = [[TRButton alloc] initWithFrame:CGRectMake(menuW*(i+1), 0, menuW, menuH)];
  100. [btn addTarget:self action:@selector(segBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
  101. [btn setTitle:btnTits[i] forState:UIControlStateNormal];
  102. [self.view addSubview:btn];
  103. [btnArr addObject:btn];
  104. btn.tag = i+1;
  105. }
  106. currentSegBtn = btnArr[sortType - 1];
  107. [currentSegBtn setTitleColor:defGreen forState:UIControlStateNormal];
  108. CGFloat y = menuH + menuY;
  109. myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, y, kSize.width, kSize.height - y - kNavOffSet) style:UITableViewStylePlain];
  110. [self.view addSubview:myTableView];
  111. [myTableView setDelegate:self];
  112. [myTableView setDataSource:self];
  113. myTableView.rowHeight = 100;
  114. myTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  115. //刷新请求
  116. holdV = [[HolderView alloc] initWithFrame:myTableView.frame];
  117. [self.view addSubview:holdV];
  118. [holdV freshBlock:^{
  119. [self gotoSearch];
  120. }];
  121. [self gotoSearch];
  122. }
  123. /**用select属性作判断,1 星级由低到高 2 由高到低
  124. currentBtn专门用来设置颜色。
  125. selected用来设置sort。1 或2
  126. */
  127. -(void)segBtnClicked:(TRButton*)sender
  128. {
  129. currentPage = 1;
  130. if (currentSegBtn) {
  131. [currentSegBtn setTitleColor:kTitleColor forState:UIControlStateNormal];
  132. }
  133. currentSegBtn = sender;
  134. [currentSegBtn setTitleColor:defGreen forState:UIControlStateNormal];
  135. [sender makeAnimate];
  136. sortType = sender.tag;
  137. sender.selected = !sender.selected;
  138. if (sender.selected) {
  139. sort = 1;
  140. }else{
  141. sort = 2;
  142. }
  143. [self gotoSearch];
  144. }
  145. -(void)clickToChoose:(UISegmentedControl *)Seg
  146. {
  147. NSInteger Index = Seg.selectedSegmentIndex;
  148. currentPage = 1;
  149. switch (Index) {
  150. case 0:
  151. self.catalog = 0;
  152. method = @"getSchInfoList";
  153. break;
  154. case 1:
  155. self.catalog = 1;
  156. method = @"getCoachInfoList";
  157. break;
  158. default:
  159. break;
  160. }
  161. [self gotoSearch];
  162. }
  163. -(void)didGetResult1:(NSString *)result andResult2:(NSString *)str2{
  164. //这里要获取dqbh和qxbh
  165. /**地区编号的数组索引
  166. */
  167. for (int i =0; i<areaArr.count; i++) {
  168. NSDictionary* dict = areaArr[i];
  169. if ([dict[@"name"] isEqualToString:result]) {
  170. dqbh = dict[@"code"];
  171. for (int j = i; j<areaArr.count; j++) {
  172. dict = areaArr[j];
  173. if ([dict[@"name"] isEqualToString:str2])
  174. {
  175. qxbh = dict[@"code"];
  176. break;
  177. }
  178. }
  179. break;
  180. }
  181. }
  182. //如果选择全部 就要将地址设为空、、
  183. if ([str2 isEqualToString:@"查不到"])
  184. {
  185. qxbh = @"";
  186. if ([result isEqualToString:@"查不到"])
  187. {
  188. dqbh = @"";
  189. }
  190. }
  191. //NSLog(@"----%@----%@----%@----%@",result,dqbh,str2,qxbh);
  192. //选完地区 要将currentPage置1 防止一直点同一个地区 然后请求不到数据
  193. currentPage = 1;
  194. [self gotoSearch];
  195. }
  196. -(void)loadArea
  197. {
  198. firArr = [NSMutableArray array];//市name【x市】
  199. secArr = [NSMutableArray array];//地区对应dic【思明区的dic【编号。name】】
  200. areaArr = [DB_Que_Helper queryArea];
  201. for (NSDictionary *dic in areaArr) {
  202. if ([dic[@"parent_code"] isEqualToString:@"2100"]) {
  203. [firArr addObject:dic[@"name"]];
  204. [secArr addObject:[NSMutableArray new]];
  205. }else{
  206. // secArr[firArr.count-1] 它们是有序的
  207. [secArr[firArr.count-1] addObject:dic[@"name"]];
  208. }
  209. }
  210. }
  211. -(void)setType:(NSInteger)type
  212. {
  213. _type = type;
  214. sortType = type;
  215. }
  216. /**现在已经没有页码了。不过还是可以用来滚得
  217. */
  218. -(void)setPageIndex:(NSInteger)pageIndex{
  219. [_btns[_pageIndex] setSelected:NO];
  220. _pageIndex = pageIndex;
  221. [_btns[_pageIndex] setSelected:YES];
  222. CGSize size = btnBar.frame.size;
  223. [UIView animateWithDuration:.2 animations:^{
  224. self->btnBar.frame = CGRectMake(size.width * self->_pageIndex, self->btnBar.frame.origin.y, size.width, size.height);
  225. }];
  226. }
  227. #pragma mark - NetWork
  228. -(void)gotoSearch
  229. {
  230. if (!isPull) {
  231. [models removeAllObjects];
  232. [myTableView reloadData];
  233. }
  234. isPull = NO;
  235. //设置默认信息
  236. if (!method)
  237. {
  238. //设置默认地区
  239. //初始化BMKLocationService
  240. [[MapManager sharedManager] updateLocationWithCompleteBlock:^(BOOL success, CLLocation * _Nonnull location) {
  241. if (success) {
  242. //储存自己当前位置
  243. self->myLocation = location.coordinate;
  244. [[MapManager sharedManager] onGetReverseGeoCodeWithLocation:location completeBlock:^(BOOL success, BMKReverseGeoCodeSearchResult * _Nonnull result, BMKSearchErrorCode error) {
  245. if (error == BMK_SEARCH_NO_ERROR)
  246. {
  247. // 街道号码 streetNumber
  248. // 街道名称 streetName
  249. // 区县名称 district
  250. // 城市名称 city
  251. // 省份名称 province;
  252. //NSLog(@"---->%@---->%@---->%@---->%@---->%@",result.addressDetail.streetNumber,result.addressDetail.streetName,result.addressDetail.district,result.addressDetail.city,result.addressDetail.province);
  253. //在此处理正常结果
  254. for (int i =0; i<self->areaArr.count; i++)
  255. {
  256. NSDictionary* dict = self->areaArr[i];
  257. if ([dict[@"name"] isEqualToString:result.addressDetail.city])
  258. {
  259. self->dqbh = dict[@"code"];
  260. }
  261. if ([dict[@"name"] isEqualToString:result.addressDetail.district])
  262. {
  263. self->qxbh = dict[@"code"];
  264. }
  265. }
  266. //如果辽宁省之外的地区 则默认为沈阳市
  267. if (self->dqbh.length < 1 && self->qxbh.length < 1) {
  268. [self->menu setPlaceHolder:@"沈阳市"];
  269. self->dqbh = @"2101";
  270. self->qxbh = @"";
  271. myDelegate.locationArray = nil;
  272. }else{
  273. [self->menu setPlaceHolder:result.addressDetail.district];
  274. myDelegate.locationArray = @[[NSString stringWithFormat:@"%f",self->myLocation.longitude],self->dqbh,[NSString stringWithFormat:@"%f",self->myLocation.latitude]];
  275. }
  276. }else{
  277. [LoadingView showMsg:@"定位失败"];
  278. [self->menu setPlaceHolder:@"沈阳市"];
  279. self->dqbh = @"2101";
  280. self->qxbh = @"";
  281. }
  282. self->method = @"getSchInfoList";
  283. self->sort = 2;
  284. /**这个在view加载的时候,由跳转者来设置。
  285. */
  286. if (self->sortType < 1)
  287. {
  288. self->sortType = 2;
  289. }
  290. [self->holdV setHidden:1];
  291. [self search];
  292. }];
  293. }else {
  294. [self->menu setPlaceHolder:@"沈阳市"];
  295. self->dqbh = @"2101";
  296. self->qxbh = @"";
  297. self->method = @"getSchInfoList";
  298. self->sort = 2;
  299. /**这个在view加载的时候,由跳转者来设置。
  300. */
  301. if (self->sortType < 1)
  302. {
  303. self->sortType = 2;
  304. }
  305. [self->holdV setHidden:1];
  306. [self search];
  307. }
  308. }];
  309. return;
  310. //得到位置之后在操作在代理中写
  311. }
  312. [holdV setHidden:1];
  313. [self search];
  314. }
  315. -(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:
  316. (BMKReverseGeoCodeSearchResult *)result errorCode:(BMKSearchErrorCode)error
  317. {
  318. searcher.delegate = nil;
  319. }
  320. /**发送请求
  321. */
  322. -(void)search
  323. {
  324. if (![Util connectedToNetWork]) {
  325. showMsgUnconnect();
  326. [holdV setHidden:0];
  327. return;
  328. }
  329. NSMutableArray *arr=[NSMutableArray array];
  330. if (dqbh == nil) {
  331. dqbh = @"";
  332. }
  333. if (qxbh == nil) {
  334. qxbh = @"";
  335. }
  336. [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"jxmc", nil]];
  337. [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:dqbh,@"dqbh", nil]];
  338. [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:qxbh ,@"qxbh", nil]];
  339. [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"starPrice", nil]];
  340. [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"endPrice", nil]];
  341. [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d",(int)sortType],@"sortType", nil]];
  342. [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d",(int)sort],@"sort", nil]];
  343. [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"1",@"isPage", nil]];
  344. [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"10",@"pageSize", nil]];
  345. [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d",currentPage],@"currentPage", nil]];
  346. if ([method isEqualToString:@"getSchInfoList"])
  347. {
  348. [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%f",myLocation.longitude],@"lng", nil]];
  349. [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%f",myLocation.latitude],@"lat", nil]];
  350. }
  351. refreshing = YES;
  352. [MBProgressHUD showLoadToView:self.view];
  353. [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary *root) {
  354. [MBProgressHUD hideHUDForView:self.view];
  355. self->refreshing = NO;
  356. NSArray *resArr = [NSArray array];
  357. if (root || [root[@"code"] isEqualToString:@"0"]) {
  358. resArr = root[@"body"];
  359. }
  360. //NSLog(@"method--%@---resArr---->%@",method,resArr);
  361. if (resArr.count > 0) {
  362. [self->models addObjectsFromArray:resArr];
  363. self->currentPage ++;
  364. }else{
  365. ShowMsg(@"已加载全部");
  366. }
  367. [self->holdV setHidden:self->models.count];
  368. [self->myTableView reloadData];
  369. }];
  370. }
  371. #pragma mark -
  372. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  373. {
  374. if (models != nil) {
  375. return models.count;
  376. }else{
  377. return 0;
  378. }
  379. }
  380. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  381. {
  382. SchCell* cell = [SchCell cellWithTableView:tableView];
  383. NSInteger row = indexPath.row;
  384. id obj = models[row];
  385. if ( 0 == self.catalog)
  386. {
  387. [cell setSchDict:obj];
  388. }
  389. else
  390. {
  391. [cell setCoaDict:obj];
  392. }
  393. return cell;
  394. }
  395. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  396. {
  397. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  398. NSDictionary* obj = models[indexPath.row];
  399. SchCell* cell = (SchCell *)[tableView cellForRowAtIndexPath:indexPath];
  400. cell.countLabel.text = [NSString stringWithFormat:@"%@人关注",[Tools isWan:[NSString stringWithFormat:@"%d",[obj[@"watch"] intValue] + 1]]];
  401. if ( 0 == self.catalog)
  402. {
  403. //NSLog(@"驾校详情");
  404. SchDetailVC* schVC = [[SchDetailVC alloc] init];
  405. schVC.schoolId = obj[@"id"];
  406. [self navPushHideTabbarToVC:schVC];
  407. }else if (1 == self.catalog){
  408. //NSLog(@"教练详情");
  409. CoachDetailVC *coachVC = [[CoachDetailVC alloc] init];
  410. coachVC.orderType = @"0";
  411. coachVC.infoDic = obj;
  412. [self navPushHideTabbarToVC:coachVC];
  413. }
  414. }
  415. -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
  416. {
  417. if (refreshing)
  418. {
  419. return;
  420. }
  421. CGPoint off = scrollView.contentOffset;
  422. if (scrollView.contentSize.height - off.y - scrollView.frame.size.height < 1)
  423. {
  424. isPull = YES;
  425. [scrollView setContentOffset:CGPointMake(off.x, off.y - 10) animated:YES];
  426. [self gotoSearch];
  427. }
  428. }
  429. @end