123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- /**
- 貌似只需要1个数组来保存数据
- */
- #import "SearchBase.h"
- #import "MyDrop.h"//自己写的下拉菜单。蛋碎
- #import "TRButton.h"
- #import "SchCell.h"
- #import "CoachDetailVC.h"
- #import "HolderView.h"
- #import "Tools.h"
- #import <BaiduMapAPI_Map/BMKMapComponent.h>
- #import <BMKLocationkit/BMKLocationComponent.h>//引入定位功能所有的头文件(新)
- #import <BaiduMapAPI_Search/BMKSearchComponent.h>
- @interface SearchBase ()<DropDelegate,UITableViewDelegate,UITableViewDataSource,BMKLocationManagerDelegate,BMKGeoCodeSearchDelegate>
- {
-
- //分段选择器下方的滚动条。
- 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; i<areaArr.count; i++) {
- NSDictionary* dict = areaArr[i];
- if ([dict[@"name"] isEqualToString:result]) {
- dqbh = dict[@"code"];
- for (int j = i; j<areaArr.count; j++) {
- dict = areaArr[j];
- if ([dict[@"name"] isEqualToString:str2])
- {
- qxbh = dict[@"code"];
- break;
- }
- }
- break;
- }
- }
- //如果选择全部 就要将地址设为空、、
- if ([str2 isEqualToString:@"查不到"])
- {
- qxbh = @"";
- if ([result isEqualToString:@"查不到"])
- {
- dqbh = @"";
- }
- }
- //NSLog(@"----%@----%@----%@----%@",result,dqbh,str2,qxbh);
- //选完地区 要将currentPage置1 防止一直点同一个地区 然后请求不到数据
- currentPage = 1;
- [self gotoSearch];
- }
- -(void)loadArea
- {
- firArr = [NSMutableArray array];//市name【x市】
- secArr = [NSMutableArray array];//地区对应dic【思明区的dic【编号。name】】
- areaArr = [DB_Que_Helper queryArea];
-
- for (NSDictionary *dic in areaArr) {
- if ([dic[@"parent_code"] isEqualToString:@"2100"]) {
- [firArr addObject:dic[@"name"]];
- [secArr addObject:[NSMutableArray new]];
- }else{
- // secArr[firArr.count-1] 它们是有序的
- [secArr[firArr.count-1] addObject:dic[@"name"]];
- }
- }
- }
- -(void)setType:(NSInteger)type
- {
- _type = type;
- sortType = type;
- }
- /**现在已经没有页码了。不过还是可以用来滚得
- */
- -(void)setPageIndex:(NSInteger)pageIndex{
- [_btns[_pageIndex] setSelected:NO];
- _pageIndex = pageIndex;
- [_btns[_pageIndex] setSelected:YES];
-
- CGSize size = btnBar.frame.size;
- [UIView animateWithDuration:.2 animations:^{
- self->btnBar.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; i<self->areaArr.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
|