IOS開發地區列表

iOS開發中我們項目中常常會使用地區列表,篩選,定位等功能,此處單單選出主要內容展示,另附有免積分demo,歡迎指正和交流

效果如下:



//遵守的代理CLLocationManagerDelegate,UIAlertViewDelegate,UITableViewDelegate,UITableViewDataSource

- (void)viewDidLoad {

    [superviewDidLoad];

    self.flageArray=[NSMutableArrayarray];

    

    _cityArray=[NSMutableArrayarray];

    _indexArray=[NSMutableArrayarray];

    _resultArray=[NSMutableArrayarray];

    //根據接口中的標示將接口中雜亂的地區重新排序添加在數組中

    for (int i='A'; i<='Z'; i++) {

        NSMutableArray * array=[NSMutableArrayarray];

        BOOL isShow=YES;

        for (int j=0; j<self.listEntitys.count; j++) {

            LocationCityEntity * entity=[self.listEntitysobjectAtIndex:j];

            int aa=[entity.zxlettercharacterAtIndex:0];

            if (aa==i) {

                

                [array addObject:entity];

                if (isShow) {

                    char bb=i;

                    [self.indexArrayaddObject:[NSStringstringWithFormat:@"%c",bb]];

                    isShow=NO;

                }

                

            }else

            {

            

            }

        }

//將排序後的數組放進地區數組中

        if (array.count>0) {

            [self.cityArrayaddObject:array];

        }

        

    }

//系統的搜索

     _searchBar=[[UISearchBaralloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,44)];

    _searchBar.placeholder=@"請輸入城市中文名稱";

    self.myTableView.tableHeaderView=_searchBar;

     _seachDisplay=[[UISearchDisplayControlleralloc]initWithSearchBar:_searchBarcontentsController:self];

    _seachDisplay.searchResultsDelegate=self;

    _seachDisplay.searchResultsDataSource=self;

    self.myTableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;

    self.myTableView.sectionIndexBackgroundColor=[UIColorclearColor];

    

}

- (void)viewWillAppear:(BOOL)animated

{

    [superviewWillAppear:animated];

//    self.tabBarController.tabBar.hidden=YES;

    self.isShow=YES;

    self.view.backgroundColor=[UIColorwhiteColor];

    self.extendedLayoutIncludesOpaqueBars=NO;

//    [MobClick beginLogPageView:@"城市切換"];//("PageOne"爲頁面名稱,可自定義)

    

}

- (void)viewWillDisappear:(BOOL)animated

{

    [superviewWillDisappear:animated];

//    [MobClick endLogPageView:@"城市切換"];

}

//定位使用的

-(CLLocationManager *)locationManager{

    

    if (!_locationManager) {

        _locationManager = [[CLLocationManageralloc]init];

        _locationManager.delegate =self;

        _locationManager.desiredAccuracy =kCLLocationAccuracyBest;

        _locationManager.distanceFilter =10.0f;

        if([_locationManagerrespondsToSelector:@selector(requestWhenInUseAuthorization)])

        {

//            [_locationManager requestAlwaysAuthorization]; //永久授權

          //  [_locationManager requestWhenInUseAuthorization]; //使用中授權

        }

    }

    return_locationManager;

}

//定位的協議方法

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{

    switch (status) {

        casekCLAuthorizationStatusNotDetermined:

            if ([managerrespondsToSelector:@selector(requestWhenInUseAuthorization)]) {

                [managerrequestWhenInUseAuthorization];

            }

            break;

        casekCLAuthorizationStatusDenied:

            locationSuccess =NO;

            cityString =@"定位失敗";

            [self.myTableViewreloadSections:[NSIndexSetindexSetWithIndex:0]withRowAnimation:UITableViewRowAnimationNone];

            break;

        default:

            break;

    }

}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

    [self.locationManagerstopUpdatingLocation];

    CLLocation *currentLocation = [locationslastObject];

    CLGeocoder *geocoder = [[CLGeocoderalloc]init];

    __weaktypeof(self) weakSelf =self;

    [geocoder reverseGeocodeLocation:currentLocationcompletionHandler:^(NSArray *placemarks,NSError *error) {

        if (placemarks.count>0&&!error) {

            CLPlacemark *placemark = placemarks[0];

//            DLog(@"%@,%@",placemark.locality,[self existCity:placemark])

            cityString = [selfexistCity:placemark];

            locationSuccess = [cityStringlength];

            if (!locationSuccess) {

                cityString = [NSStringstringWithFormat:@"暫不支持%@",[placemark.localitysubstringToIndex:placemark.locality.length-1]];

            }

            [weakSelf.myTableViewreloadSections:[NSIndexSetindexSetWithIndex:0]withRowAnimation:UITableViewRowAnimationNone];

        }else{

            locationSuccess =NO;

            cityString =@"定位失敗";

            [weakSelf.myTableViewreloadSections:[NSIndexSetindexSetWithIndex:0]withRowAnimation:UITableViewRowAnimationNone];

        }

    }];

}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{

    [self.locationManagerstopUpdatingLocation];

//    [SVProgressHUD showErrorWithStatus:error.localizedDescription];

    locationSuccess =NO;

    cityString =@"定位失敗";

//刷新0 組的方法

    [self.myTableViewreloadSections:[NSIndexSetindexSetWithIndex:0]withRowAnimation:UITableViewRowAnimationNone];

}

//轉換城市使用的方法

-(NSString *)existCity:(CLPlacemark *)city{

    for (int i =0; i<self.listEntitys.count; i++) {

        LocationCityEntity *entity = [self.listEntitysobjectAtIndex:i];

        if ([city.localityhasPrefix:entity.zxprovince]) {

//            [GlobalData setLocationCity:entity.zxprovince];

            return entity.zxprovince;

        }elseif ([city.addressDictionary[@"State"]hasPrefix:entity.zxprovince])

        {

//            [GlobalData setLocationCity:[NSString stringWithFormat:@"%@,%@",entity.zxprovince,city.locality]];

            return [NSStringstringWithFormat:@"%@,%@",entity.zxprovince,city.locality];

        }

    }

    return [NSStringstringWithFormat:@"暫時沒有開通%@站點,請選擇其他城市",city];

}

//傳值的效果

-(void)pushCityEntiyArr:(NSMutableArray *)citiyEntiyArr cityStr:(NSString *)cityStr viewType:(NSString *)viewType

{

    _listEntitys = [NSMutableArrayarray];

    _listEntitys = citiyEntiyArr;

    cityString = cityStr;

    _viewType = viewType;

    if ([cityStrisEqualToString:@"定位失敗"]||[cityStrisEqualToString:@""])

    {

        [self.locationManagerstartUpdatingLocation];

    }

    [self.myTableViewreloadData];

}

//返回組數

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    

    if (tableView!=self.myTableView)

    {

        return1;

    }else

    {

      returnself.cityArray.count+1+1;

    

    }


    

   


}

//返回行數

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    

    if (tableView!=self.myTableView)

    {

        //返回搜索結果的值

        //現將之前搜索的內容移除

        [_resultArrayremoveAllObjects];

        

        for (NSArray * subArrayinself.cityArray)

        {

            for (LocationCityEntity * entity in subArray)

            {

                NSRange range=[entity.zxprovincerangeOfString:_searchBar.text];

                

                if (range.location!=NSNotFound)

                {

                    [_resultArrayaddObject:entity];

                }

            }

        }

        

        return_resultArray.count;

        

    }else

    {

        if(section ==0)

        {

            return1;

        }elseif(section ==1)

        {

            return1;

        }else

        {

            return [[self.cityArrayobjectAtIndex:section-2]count];

        }


    

    }


    

}

//返回行高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (indexPath.section==1) {

        

        returnceilf(((float)[GlobalDatahotCityArr].count/3))*40.0;

        

    }else

    {

        return50.0;

    }

}

//頭視圖高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

    return40*SCREEN_WIDTH/414;

}

//組頭顯示的內容

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    if (tableView==self.myTableView) {

        UIView *view = [[UIViewalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,40*SCREEN_WIDTH/414)];

        [view setBackgroundColor:[UIColorcolorWithRed:233/255.fgreen:234/255.fblue:235/255.falpha:1]];

        UILabel *titleLabel =[[UILabelalloc]initWithFrame:CGRectMake(15,5,200,21)];

        if (section ==0) {

            titleLabel.text =@"當前定位";

        }elseif (section==1)

        {

        titleLabel.text=@"熱門城市";

        

        }else

        {

            //城市列表

            titleLabel.text = [self.indexArrayobjectAtIndex:section-2];

        }

        [view addSubview:titleLabel];

        return view;


    }else

    {

        UIView *view = [[UIViewalloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,40*SCREEN_WIDTH/414)];

        [view setBackgroundColor:[UIColorcolorWithRed:233/255.fgreen:234/255.fblue:235/255.falpha:1]];

        UILabel *titleLabel =[[UILabelalloc]initWithFrame:CGRectMake(15,5,200,21)];

        titleLabel.text=@"搜索結果";

        [view addSubview:titleLabel];

        return view;


    }

}

//cell的顯示內容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    UITableViewCell *cell;

    

    if (tableView!=self.myTableView) {

        staticNSString* cellId=@"cellId";

        if (cell==nil) {

            cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellId];

        }

         cell.textLabel.text=[[_resultArrayobjectAtIndex:indexPath.row]zxprovince];

    }else

    {

        if (indexPath.section ==0) {

            diweiCell *myCell = [self.myTableViewdequeueReusableCellWithIdentifier:@"diweiCell"forIndexPath:indexPath];

            myCell.locationLabel.text = [GlobalDatalocationCity];

            cell = myCell;

        }elseif (indexPath.section ==1) {

            hotCityCell *myCell = [self.myTableViewdequeueReusableCellWithIdentifier:@"hotCityCell"forIndexPath:indexPath];

            [myCell setMycollectionView];

            myCell.hotArray=[GlobalDatahotCityArr];

            [myCell.MyCollectionViewreloadData];

            __weaktypeof(self) weakSelf=self;

            myCell.didCollection=^(LocationCityEntity * entity,NSIndexPath *indexpa)

            {

                typeof(self) strongSelf=weakSelf;

                        NSString *msg = [NSStringstringWithFormat:@"切換地區將會登出當前賬號,是否要切換至%@站?",entity.zxprovince];

                        UIAlertView *alert =[[UIAlertViewalloc]initWithTitle:@"提示"message:msgdelegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"確定",nil];

                        alert.tag =indexpa.row;

                        strongSelf.isShow=NO;

                        [alert show];

                

            };

            cell = myCell;

        }else

        {

            cityCell *myCell = [self.myTableViewdequeueReusableCellWithIdentifier:@"cityCell"forIndexPath:indexPath];

            if (self.cityArray.count>0) {

                LocationCityEntity *entity = [[self.cityArrayobjectAtIndex:indexPath.section-2]objectAtIndex:indexPath.row];

                myCell.cityLabel.text = entity.zxprovince;

                [self.flageArrayaddObject:entity.zxletter];

            }

           cell = myCell;

        }

        

   }

    return cell;

}

//cell的點擊事件

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    tableView.tag=1114;

    if (tableView!=self.myTableView)

    {

        if (self.resultArray.count>0)

        {

            

            NSString *city = [[self.resultArrayobjectAtIndex:indexPath.row]zxprovince];

            LocationCityEntity * entity;

            for (int i=0; i<self.listEntitys.count; i++) {

               LocationCityEntity *  entity1 =[self.listEntitysobjectAtIndex:i];

                if ([entity1.zxprovinceisEqualToString:city]) {

                    entity=entity1;

                }else

                {

                

                }

            }

           

            //切換地區操作

                NSString *msg = [NSStringstringWithFormat:@"切換地區將會登出當前賬號,是否要切換至%@站?",city];

                UIAlertView *alert =[[UIAlertViewalloc]initWithTitle:@"提示"message:msgdelegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"確定",nil];

                alert.tag = indexPath.row;

                alert.message=msg;

                [alert show];

            

            

        }

    


        

        

    }else{

    if(indexPath.section >1)

     {

        if (self.cityArray.count>0)

        {

               LocationCityEntity *entity = [[self.cityArrayobjectAtIndex:indexPath.section-2]objectAtIndex:indexPath.row];

            

                NSString *msg = [NSStringstringWithFormat:@"切換地區將會登出當前賬號,是否要切換至%@站?",entity.zxprovince];

                UIAlertView *alert =[[UIAlertViewalloc]initWithTitle:@"提示"message:msgdelegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"確定",nil];

                alert.tag = indexPath.row;

                alert.message=msg;

                self.indexTag = (int)indexPath.section-2;

                [alert show];

           

        }

    }

    }

}

#pragma mark ----小標誌代碼

-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

    if (tableView==self.myTableView)

    {

        NSMutableArray * array=[NSMutableArrayarrayWithArray:self.indexArray ];

        //[array insertObject:@"" atIndex:0];

        [array insertObject:@""atIndex:0];

        return  array;

    }else

    {

        returnnil;

    }

   

}

//彈出信息之後的操作

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    

    UITableView * _tableView=(UITableView*)[self.viewviewWithTag:1114];

    

    if (_tableView==self.myTableView) {

        if (buttonIndex ==1) {

            

            

        }


    }elseif(self.isShow==NO)

    {

        if (buttonIndex ==1) {

           

        }


    

    }else

    {

        if (buttonIndex ==1) {

           

            

            

            

        }


    }

   

}

附件:地區篩選的demo(免積分下載),使用了block ,類擴展,本地保存等(只爲共同學習)

http://download.csdn.net/detail/bddzzw/9597894


發佈了73 篇原創文章 · 獲贊 4 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章