溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在iOS中實(shí)現(xiàn)一個(gè)三級(jí)聯(lián)動(dòng)選擇器

發(fā)布時(shí)間:2021-04-12 17:31:55 來源:億速云 閱讀:128 作者:Leah 欄目:移動(dòng)開發(fā)

本篇文章為大家展示了怎么在iOS中實(shí)現(xiàn)一個(gè)三級(jí)聯(lián)動(dòng)選擇器,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

1)怎么設(shè)置默認(rèn)值,關(guān)鍵代碼

[self.pickerView selectRow:xxx inComponent:xxx animated:YES];

2)怎么讓三級(jí)之間聯(lián)動(dòng) ,關(guān)鍵代碼

[self pickerView:self.pickerView didSelectRow:0 inComponent:0 ];//聯(lián)動(dòng)輪子1  必須得本輪有數(shù)據(jù)后觸發(fā)否則crash

先看下效果圖

怎么在iOS中實(shí)現(xiàn)一個(gè)三級(jí)聯(lián)動(dòng)選擇器

關(guān)于設(shè)置默認(rèn)值,三級(jí)聯(lián)動(dòng),用UIPickView的話就是有3個(gè)輪子(component),首先我們要想到,第一次向后臺(tái)發(fā)起請求,我們只能獲取到第0個(gè)component的數(shù)據(jù),只有當(dāng)你滾動(dòng)輪子的時(shí)候才會(huì)獲取到省的Id發(fā)起請求來獲得該省的市的數(shù)據(jù),也就是第1個(gè)component的數(shù)據(jù),依此類推,滾動(dòng)第1個(gè)component發(fā)起請求來獲取第2個(gè)component的數(shù)據(jù),因此,pickView的監(jiān)聽輪子滾動(dòng)的代理起了重要作用

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;

我們通過接口獲取第0個(gè)component的數(shù)據(jù),這邊是后臺(tái)規(guī)定的使用id=0,獲取到以后,默認(rèn)選中第0個(gè)component的第0個(gè)row并主動(dòng)調(diào)用觸發(fā)pick的輪子滾動(dòng)代理來聯(lián)動(dòng)第1個(gè)component【要在獲取數(shù)據(jù)成功后再執(zhí)行這部操作,因此放在數(shù)據(jù)請求成功的回調(diào)內(nèi)】,代碼為

 [self pickerView:self.pickerView didSelectRow:0 inComponent:0 ];

在各輪子滾動(dòng)過程中,用一個(gè)中間值

_selectedRow0記錄下第0個(gè)component的選中行

_selectedRow1記錄下第1個(gè)component的選中行

_selectedRow2記錄下第2個(gè)component的選中行,

這里需要記住,滾動(dòng)某個(gè)輪子只能對(duì)它后面的輪子產(chǎn)生影響,所以當(dāng)滾動(dòng)第0個(gè)component的時(shí)候使_selectedRow1,_selectedRow2均置為0,這里注意,上面提到的

默認(rèn)選中第0個(gè)component的第0個(gè)row并主動(dòng)調(diào)用觸發(fā)pick的輪子滾動(dòng)代理來聯(lián)動(dòng)第1個(gè)component

要先將輪子上的數(shù)據(jù)渲染好,設(shè)置好默認(rèn)值才能主動(dòng)調(diào)用監(jiān)聽輪子滾動(dòng)的代理,否則會(huì)導(dǎo)致崩潰,另一個(gè)防崩潰的點(diǎn)如下圖

怎么在iOS中實(shí)現(xiàn)一個(gè)三級(jí)聯(lián)動(dòng)選擇器

發(fā)現(xiàn)第三級(jí)沒數(shù)據(jù)的時(shí)候,如果你在代碼里沒加【安全措施】,那也會(huì)導(dǎo)致崩潰,要在請求到第三級(jí)的數(shù)據(jù)后做下判斷,如果個(gè)數(shù)為空,將該級(jí)對(duì)應(yīng)的數(shù)據(jù)源置為nil。(其它兩級(jí)的輪子最好也加判斷)

最后,由于這是個(gè)封裝的類,最終要得到選中的詳細(xì)信息,可通過代理或block傳值給controller。

又是你們最喜歡show code環(huán)節(jié):

.h文件

#import <UIKit/UIKit.h>

//定制代理協(xié)議
@protocol ZLMAddressPickerViewDelegate <NSObject>

- (void)addressPickerViewDidSelected:(NSString *)areaName;//點(diǎn)擊上方完成按鈕的代理傳回拼接好的選中地址

- (void)addressPickerViewDidClose;//點(diǎn)擊關(guān)閉代理

@end

@interface ZLMAddressPickerView : UIView

@property (weak, nonatomic) id<ZLMAddressPickerViewDelegate> delegate;

@end

.m文件

#import "ZLMAddressPickerView.h"
#import "AFHttpUtils.h"
#import "AreaModel.h"
@interface ZLMAddressPickerView () <UIPickerViewDataSource, UIPickerViewDelegate>
@property (strong, nonatomic) UIPickerView *pickerView;
@property (strong, nonatomic) AreaModel  *provBridge;
@property (strong, nonatomic) AreaModel  *cityBridge;
@property (strong, nonatomic) AreaModel  *areaBridge;
@property (copy, nonatomic) NSArray<Area *> * provDataArr;//省數(shù)組
@property (copy, nonatomic) NSArray<Area *> * cityDataArr;//市數(shù)組
@property (copy, nonatomic) NSArray<Area *> * areaDataArr;//區(qū)數(shù)組
@end

@implementation ZLMAddressPickerView
{
  NSInteger _selectRow0;//記錄第0個(gè)輪子的選擇行
  NSInteger _selectRow1;
  NSInteger _selectRow2;
  NSString *_areaString;//最后要傳回的詳細(xì)地址拼接字符串
  Area *_proModel;//記錄下選中省的數(shù)據(jù)
  Area *_cityModel;
  Area *_areaModel;

}

- (instancetype)initWithFrame:(CGRect)frame
{
  self = [super initWithFrame:frame];
  if (self) {
    [self setup];
  }
  return self;
}

- (void)setup {
 
  _selectRow0 = 0;
  _selectRow1 = 0;
  _selectRow2 = 0;

  self.backgroundColor  = [UIColor whiteColor];
  UIToolbar *toolbar   = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), 44)];
  toolbar.backgroundColor = [UIColor whiteColor];
  [self addSubview:toolbar];
  
  UIBarButtonItem *closeItem   = [[UIBarButtonItem alloc] initWithTitle:@"關(guān)閉" style:UIBarButtonItemStylePlain target:self action:@selector(selectAddressClose)];
  UIBarButtonItem *completeItem  = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(selectAddressComplete)];
  UIBarButtonItem *spaceItem   = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
  toolbar.items                     = @[closeItem, spaceItem, completeItem];
  
  self.pickerView.frame = CGRectMake(0, 44, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds) - 44);

  [self addSubview:self.pickerView];
  
  [self downloadProv];
  
}

#pragma mark - http methods

/*省*/
- (void)downloadProv {
  
  NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary: @{@"id":@(0)} ];
  
  [AFHttpUtils sendPostTaskWithUrl:[NSString stringWithFormat:@"%@/app/member/area",BASE_DOMAIN_URL] paramenters:dic successHandle:^(NSURLSessionDataTask *task, id responseObject) {
    
     NSLog(@"PROV:%@",responseObject);
    
    self.provBridge = [AreaModel mj_objectWithKeyValues:responseObject];
    
    if (self.provBridge.error_code==0) {
      
      self.provDataArr=self.provBridge.data;
      
       [self pickerView:self.pickerView didSelectRow:0 inComponent:0 ];//聯(lián)動(dòng)輪子1 必須得本輪有數(shù)據(jù)后才能觸發(fā)didselect
      
      [self.pickerView reloadAllComponents];
  
    }
  } errorHandle:^(NSError *error) {
    
  }];

}
/*市*/
- (void)downloadCityWithId:(NSInteger)provId {
  
  NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary: @{@"id":@(provId)} ];
  
  [AFHttpUtils sendPostTaskWithUrl:[NSString stringWithFormat:@"%@/app/member/area",BASE_DOMAIN_URL] paramenters:dic successHandle:^(NSURLSessionDataTask *task, id responseObject) {
    
    NSLog(@"CITY:%@",responseObject);
    
    self.cityBridge = [AreaModel mj_objectWithKeyValues:responseObject];
  
    if (self.cityBridge.error_code==0) {
      
      self.cityDataArr=self.cityBridge.data;
      
      [self.pickerView reloadComponent:1];
      
      [self.pickerView selectRow:0 inComponent:1 animated:YES];//默認(rèn)選擇row0
      
      [self pickerView:self.pickerView didSelectRow:0 inComponent:1 ];//聯(lián)動(dòng)輪子2 必須得本輪有數(shù)據(jù)后才能觸發(fā)didselect
      
      _cityModel = self.cityDataArr[_selectRow1];
      
      [self downloadAreaWithId:_cityModel.area_id];
      
    }
  } errorHandle:^(NSError *error) {
    
  }];
  
}
/*區(qū)*/
- (void)downloadAreaWithId:(NSInteger)cityId {
  
  NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary: @{@"id":@(cityId)} ];
  
  [AFHttpUtils sendPostTaskWithUrl:[NSString stringWithFormat:@"%@/app/member/area",BASE_DOMAIN_URL] paramenters:dic successHandle:^(NSURLSessionDataTask *task, id responseObject) {
    
    NSLog(@"AREA:%@",responseObject);
    
    self.areaBridge = [AreaModel mj_objectWithKeyValues:responseObject];
    
    if (self.areaBridge.error_code==0&&self.areaBridge.data.count>0) {
      
      self.areaDataArr=self.areaBridge.data;
      
    }else{
      
      self.areaDataArr=nil;
      
    }
    [self.pickerView reloadComponent:2];
    
    [self.pickerView selectRow:0 inComponent:2 animated:YES];
    
    [self pickerView:self.pickerView didSelectRow:0 inComponent:2 ];
  
  } errorHandle:^(NSError *error) {
    
  }];
  
}
#pragma mark - events response
- (void)selectAddressComplete {
  [self.delegate addressPickerViewDidSelected:_areaString];
}

- (void)selectAddressClose {
  [self.delegate addressPickerViewDidClose];
}

#pragma mark - UIPickerViewDataSource

//確定picker的輪子個(gè)數(shù)
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
  
  return 3;
}

//確定picker的每個(gè)輪子的item數(shù)
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
  if (component==0) {
     return self.provDataArr.count;
    
  }else if(component==1){
    return self.cityDataArr.count;
    
  }else{
    return self.areaDataArr.count;
    
  }
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
  return 36;
}

//確定每個(gè)輪子的每一項(xiàng)顯示什么內(nèi)容
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
  
  NSDictionary * attrDic = @{NSForegroundColorAttributeName:[UIColor blackColor],
                NSFontAttributeName:[UIFont systemFontOfSize:12]};
  Area *area;
  if (component==0) {
    area = self.provDataArr[row];
    
  }else if(component==1){
    area = self.cityDataArr[row];
    
  }else{
    area = self.areaDataArr[row];
   
  }
   NSAttributedString * attrString = [[NSAttributedString alloc] initWithString:area.name
                         attributes:attrDic];
  return attrString;
}

//監(jiān)聽輪子的移動(dòng)
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
  
  if (component==0) {
    
    _selectRow0 = [pickerView selectedRowInComponent:0];
    
    _selectRow1 = 0;
    
    _selectRow2 = 0;
    
    _proModel  = self.provDataArr[_selectRow0];
    
    [self downloadCityWithId:_proModel.area_id];
      
  }else if(component==1){
    
    _selectRow1 = [pickerView  selectedRowInComponent:1];
    
    _selectRow2 = 0;
    
    _cityModel = self.cityDataArr[_selectRow1];
    
     [self downloadAreaWithId:_cityModel.area_id];
    
  }else{
    
    _selectRow2 = [pickerView selectedRowInComponent:2];

    if (self.areaDataArr&&self.areaDataArr.count>0) {

       _areaModel = self.areaDataArr[_selectRow2];
    }else{
      _areaModel = nil;
    }
  }
   if(_areaModel==nil){
    _areaString = [NSString stringWithFormat:@"%@ %@",_proModel.name,_cityModel.name];
   }else{
   _areaString = [NSString stringWithFormat:@"%@ %@ %@",_proModel.name,_cityModel.name,_areaModel.name];
   }
}

#pragma mark - getters and setters
- (UIPickerView *)pickerView {
  if (_pickerView == nil) {
    _pickerView = [[UIPickerView alloc] init];
    _pickerView.delegate  = self;
    _pickerView.dataSource = self;
    
  }
  return _pickerView;
}

@end

最后在controller中調(diào)用

(1)導(dǎo)入

#import "ZLMAddressPickerView.h"

(2)定義一個(gè)對(duì)象并遵守代理協(xié)議

@property (strong, nonatomic) ZLMAddressPickerView *addressPickerView;

(3)懶加載生成對(duì)象(個(gè)人習(xí)慣)

- (ZLMAddressPickerView *)addressPickerView {
  if (!_addressPickerView) {
    _addressPickerView     = [[ZLMAddressPickerView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-244-64, SCREEN_WIDTH, 244)];
    _addressPickerView.delegate = self;
  }
  return _addressPickerView;
}

(4)在點(diǎn)擊跳出三級(jí)聯(lián)動(dòng)選擇器的地方

 [self.view addSubview:self.addressPickerView];

(5)別忘了實(shí)現(xiàn)代理

#pragma mark - ZLMAddressPickerViewDelegate

- (void)addressPickerViewDidSelected:(NSString *)areaName {

  self.areaLabel.text = areaName;//將傳回的詳細(xì)地址字符串賦值

  [self addressPickerViewDidClose];
}

- (void)addressPickerViewDidClose {

  [self.addressPickerView removeFromSuperview];
}

上述內(nèi)容就是怎么在iOS中實(shí)現(xiàn)一個(gè)三級(jí)聯(lián)動(dòng)選擇器,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

ios
AI