/** 貌似只需要1个数组来保存数据 */ #import "SearchBase.h" #import "MyDrop.h"//自己写的下拉菜单。蛋碎 #import "TRButton.h" #import "SchCell.h" #import "CoachDetailVC.h" #import "HolderView.h" #import "Tools.h" #import #import //引入定位功能所有的头文件(新) #import @interface SearchBase () { //分段选择器下方的滚动条。 UIView* btnBar; //地区选择器 MyDrop* menu; HolderView* holdV; NSMutableArray* btnArr; //这里保存的是myDrop下拉菜单的数据源 NSMutableArray *firArr,*secArr; //所有地区名称,加code。pcode NSArray* areaArr; /**排列类型 1 价格 2 星级 3 人气 */ // ENUMSortType sortType; NSInteger sortType; /** 排列方式 1 星级由低到高 2 由高到低 */ NSInteger sort; TRButton* currentSegBtn; NSString* dqbh,*qxbh; //搜索用的方法名。 NSString* method; BOOL refreshing; /**如果是通过上拉来实现。刷新。就是YES。 如果是NO。刷新需要移除所有数据 */ BOOL isPull; int currentPage; NSMutableArray* models; //地图定位 CLLocationCoordinate2D myLocation; } /**每次修改搜索条件后。要移除所有。 */ /**分段选择器的按钮 */ @property (nonatomic, strong) NSMutableArray *btns; @property (nonatomic, assign) NSInteger pageIndex; @end @implementation SearchBase - (void)viewDidLoad { [super viewDidLoad]; [self.view setBackgroundColor:[UIColor whiteColor]]; self.navigationController.navigationBar.translucent = NO; [self configNavigationBar]; self.pageIndex = 0; [self baseInit]; } -(void)viewWillDisappear:(BOOL)animated { self.hidesBottomBarWhenPushed = NO; [super viewWillDisappear:animated]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } #pragma mark - -(void)baseInit { currentPage = 1; self.catalog = 0; models = [NSMutableArray array]; NSArray* segmentArray = @[@"驾校排行榜",@"教练排行榜"]; UISegmentedControl *segmentedControl =[[UISegmentedControl alloc]initWithItems:segmentArray]; segmentedControl.frame = CGRectMake(0, 0, kSize.width - 120, 35); segmentedControl.selectedSegmentIndex = 0; segmentedControl.tintColor = defGreen; [segmentedControl addTarget:self action:@selector(clickToChoose:)forControlEvents:UIControlEventValueChanged]; [self.navigationItem setTitleView:segmentedControl]; CGFloat menuW = kSize.width/4; CGFloat menuY = 0; CGFloat menuH = 40; menu = [[MyDrop alloc] initWithFrame:CGRectMake(0, menuY, menuW, menuH)]; [self.view addSubview:menu]; [self loadArea]; menu.firArr = firArr; menu.secArr = secArr; menu.delegate = self; //分段选择器的按钮 btnArr = [NSMutableArray array]; NSArray* btnTits = @[@"价格",@"星级",@"人气"]; for (int i=0; i < btnTits.count;i++) { TRButton* btn = [[TRButton alloc] initWithFrame:CGRectMake(menuW*(i+1), 0, menuW, menuH)]; [btn addTarget:self action:@selector(segBtnClicked:) forControlEvents:UIControlEventTouchUpInside]; [btn setTitle:btnTits[i] forState:UIControlStateNormal]; [self.view addSubview:btn]; [btnArr addObject:btn]; btn.tag = i+1; } currentSegBtn = btnArr[sortType - 1]; [currentSegBtn setTitleColor:defGreen forState:UIControlStateNormal]; CGFloat y = menuH + menuY; myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, y, kSize.width, kSize.height - y - kNavOffSet) style:UITableViewStylePlain]; [self.view addSubview:myTableView]; [myTableView setDelegate:self]; [myTableView setDataSource:self]; myTableView.rowHeight = 100; myTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; //刷新请求 holdV = [[HolderView alloc] initWithFrame:myTableView.frame]; [self.view addSubview:holdV]; [holdV freshBlock:^{ [self gotoSearch]; }]; [self gotoSearch]; } /**用select属性作判断,1 星级由低到高 2 由高到低 currentBtn专门用来设置颜色。 selected用来设置sort。1 或2 */ -(void)segBtnClicked:(TRButton*)sender { currentPage = 1; if (currentSegBtn) { [currentSegBtn setTitleColor:kTitleColor forState:UIControlStateNormal]; } currentSegBtn = sender; [currentSegBtn setTitleColor:defGreen forState:UIControlStateNormal]; [sender makeAnimate]; sortType = sender.tag; sender.selected = !sender.selected; if (sender.selected) { sort = 1; }else{ sort = 2; } [self gotoSearch]; } -(void)clickToChoose:(UISegmentedControl *)Seg { NSInteger Index = Seg.selectedSegmentIndex; currentPage = 1; switch (Index) { case 0: self.catalog = 0; method = @"getSchInfoList"; break; case 1: self.catalog = 1; method = @"getCoachInfoList"; break; default: break; } [self gotoSearch]; } -(void)didGetResult1:(NSString *)result andResult2:(NSString *)str2{ //这里要获取dqbh和qxbh /**地区编号的数组索引 */ for (int i =0; ibtnBar.frame = CGRectMake(size.width * self->_pageIndex, self->btnBar.frame.origin.y, size.width, size.height); }]; } #pragma mark - NetWork -(void)gotoSearch { if (!isPull) { [models removeAllObjects]; [myTableView reloadData]; } isPull = NO; //设置默认信息 if (!method) { //设置默认地区 //初始化BMKLocationService [[MapManager sharedManager] updateLocationWithCompleteBlock:^(BOOL success, CLLocation * _Nonnull location) { if (success) { //储存自己当前位置 self->myLocation = location.coordinate; [[MapManager sharedManager] onGetReverseGeoCodeWithLocation:location completeBlock:^(BOOL success, BMKReverseGeoCodeSearchResult * _Nonnull result, BMKSearchErrorCode error) { if (error == BMK_SEARCH_NO_ERROR) { // 街道号码 streetNumber // 街道名称 streetName // 区县名称 district // 城市名称 city // 省份名称 province; //NSLog(@"---->%@---->%@---->%@---->%@---->%@",result.addressDetail.streetNumber,result.addressDetail.streetName,result.addressDetail.district,result.addressDetail.city,result.addressDetail.province); //在此处理正常结果 for (int i =0; iareaArr.count; i++) { NSDictionary* dict = self->areaArr[i]; if ([dict[@"name"] isEqualToString:result.addressDetail.city]) { self->dqbh = dict[@"code"]; } if ([dict[@"name"] isEqualToString:result.addressDetail.district]) { self->qxbh = dict[@"code"]; } } //如果辽宁省之外的地区 则默认为沈阳市 if (self->dqbh.length < 1 && self->qxbh.length < 1) { [self->menu setPlaceHolder:@"沈阳市"]; self->dqbh = @"2101"; self->qxbh = @""; myDelegate.locationArray = nil; }else{ [self->menu setPlaceHolder:result.addressDetail.district]; myDelegate.locationArray = @[[NSString stringWithFormat:@"%f",self->myLocation.longitude],self->dqbh,[NSString stringWithFormat:@"%f",self->myLocation.latitude]]; } }else{ [LoadingView showMsg:@"定位失败"]; [self->menu setPlaceHolder:@"沈阳市"]; self->dqbh = @"2101"; self->qxbh = @""; } self->method = @"getSchInfoList"; self->sort = 2; /**这个在view加载的时候,由跳转者来设置。 */ if (self->sortType < 1) { self->sortType = 2; } [self->holdV setHidden:1]; [self search]; }]; }else { [self->menu setPlaceHolder:@"沈阳市"]; self->dqbh = @"2101"; self->qxbh = @""; self->method = @"getSchInfoList"; self->sort = 2; /**这个在view加载的时候,由跳转者来设置。 */ if (self->sortType < 1) { self->sortType = 2; } [self->holdV setHidden:1]; [self search]; } }]; return; //得到位置之后在操作在代理中写 } [holdV setHidden:1]; [self search]; } -(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result: (BMKReverseGeoCodeSearchResult *)result errorCode:(BMKSearchErrorCode)error { searcher.delegate = nil; } /**发送请求 */ -(void)search { if (![Util connectedToNetWork]) { showMsgUnconnect(); [holdV setHidden:0]; return; } NSMutableArray *arr=[NSMutableArray array]; if (dqbh == nil) { dqbh = @""; } if (qxbh == nil) { qxbh = @""; } [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"jxmc", nil]]; [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:dqbh,@"dqbh", nil]]; [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:qxbh ,@"qxbh", nil]]; [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"starPrice", nil]]; [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"endPrice", nil]]; [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d",(int)sortType],@"sortType", nil]]; [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d",(int)sort],@"sort", nil]]; [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"1",@"isPage", nil]]; [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"10",@"pageSize", nil]]; [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%d",currentPage],@"currentPage", nil]]; if ([method isEqualToString:@"getSchInfoList"]) { [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%f",myLocation.longitude],@"lng", nil]]; [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%f",myLocation.latitude],@"lat", nil]]; } refreshing = YES; [MBProgressHUD showLoadToView:self.view]; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary *root) { [MBProgressHUD hideHUDForView:self.view]; self->refreshing = NO; NSArray *resArr = [NSArray array]; if (root || [root[@"code"] isEqualToString:@"0"]) { resArr = root[@"body"]; } //NSLog(@"method--%@---resArr---->%@",method,resArr); if (resArr.count > 0) { [self->models addObjectsFromArray:resArr]; self->currentPage ++; }else{ ShowMsg(@"已加载全部"); } [self->holdV setHidden:self->models.count]; [self->myTableView reloadData]; }]; } #pragma mark - - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (models != nil) { return models.count; }else{ return 0; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { SchCell* cell = [SchCell cellWithTableView:tableView]; NSInteger row = indexPath.row; id obj = models[row]; if ( 0 == self.catalog) { [cell setSchDict:obj]; } else { [cell setCoaDict:obj]; } return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; NSDictionary* obj = models[indexPath.row]; SchCell* cell = (SchCell *)[tableView cellForRowAtIndexPath:indexPath]; cell.countLabel.text = [NSString stringWithFormat:@"%@人关注",[Tools isWan:[NSString stringWithFormat:@"%d",[obj[@"watch"] intValue] + 1]]]; if ( 0 == self.catalog) { //NSLog(@"驾校详情"); SchDetailVC* schVC = [[SchDetailVC alloc] init]; schVC.schoolId = obj[@"id"]; [self navPushHideTabbarToVC:schVC]; }else if (1 == self.catalog){ //NSLog(@"教练详情"); CoachDetailVC *coachVC = [[CoachDetailVC alloc] init]; coachVC.orderType = @"0"; coachVC.infoDic = obj; [self navPushHideTabbarToVC:coachVC]; } } -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { if (refreshing) { return; } CGPoint off = scrollView.contentOffset; if (scrollView.contentSize.height - off.y - scrollView.frame.size.height < 1) { isPull = YES; [scrollView setContentOffset:CGPointMake(off.x, off.y - 10) animated:YES]; [self gotoSearch]; } } @end