亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS三級聯(lián)動選擇器的實(shí)現(xiàn)代碼示例

 更新時間:2017年09月05日 10:29:16   作者:APP叫我取個帥氣的昵稱  
本篇文章主要介紹了iOS三級聯(lián)動選擇器的實(shí)現(xiàn)代碼示例,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下

無聊ing...封裝個省市區(qū)三級聯(lián)動選擇器的小demo吧。

上家公司的三級地區(qū)選擇器的數(shù)據(jù)是一次性通過網(wǎng)絡(luò)請求就能獲取到的,但新東家這邊并不是,而是先選擇了省獲取省的Id再去獲取市,再通過得到市的Id獲取區(qū)域,show code之前,先看下需要考慮的幾個點(diǎn):

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

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

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

復(fù)制代碼 代碼如下:

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

先看下效果圖


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

復(fù)制代碼 代碼如下:

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

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

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

在各輪子滾動過程中,用一個中間值

_selectedRow0記錄下第0個component的選中行

_selectedRow1記錄下第1個component的選中行

_selectedRow2記錄下第2個component的選中行,

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

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

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


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

最后,由于這是個封裝的類,最終要得到選中的詳細(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個輪子的選擇行
  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)動輪子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)動輪子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的輪子個數(shù)
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
  
  return 3;
}

//確定picker的每個輪子的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;
}

//確定每個輪子的每一項顯示什么內(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)聽輪子的移動
- (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)定義一個對象并遵守代理協(xié)議

@property (strong, nonatomic) ZLMAddressPickerView *addressPickerView;

(3)懶加載生成對象(個人習(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)擊跳出三級聯(lián)動選擇器的地方

 [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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS讓軟鍵盤消失的簡單方法

    iOS讓軟鍵盤消失的簡單方法

    一些文本輸入控件等待輸入時會彈出軟鍵盤,我們可以設(shè)置這些控件的Did End On Exit之類的回調(diào)方法以在用戶點(diǎn)擊軟鍵盤上的done或return之列的按鍵時收起鍵盤
    2016-02-02
  • 簡單講解Objective-C的基本特性及其內(nèi)存管理方式

    簡單講解Objective-C的基本特性及其內(nèi)存管理方式

    這篇文章主要介紹了簡單講解Objective-C的基本特性及其內(nèi)存管理方式,雖然Swift語言出現(xiàn)后iOS和Mac OS應(yīng)用開發(fā)方面Objective-C正在成為過去時,但現(xiàn)有諸多項目仍然在使用,需要的朋友可以參考下
    2016-01-01
  • IOS開發(fā)之路--C語言基礎(chǔ)知識

    IOS開發(fā)之路--C語言基礎(chǔ)知識

    當(dāng)前移動開發(fā)的趨勢已經(jīng)勢不可擋,這個系列希望淺談一下個人對IOS開發(fā)的一些見解,今天我們從最基礎(chǔ)的C語言開始,C語言部分我將分成幾個章節(jié)去說,今天我們簡單看一下C的一些基礎(chǔ)知識,更高級的內(nèi)容我將放到后面的文章中。
    2014-08-08
  • Objective-C編程中語句和變量的一些編寫規(guī)范建議

    Objective-C編程中語句和變量的一些編寫規(guī)范建議

    這篇文章主要介紹了Objective-C編程中語句和變量的一些編寫規(guī)范建議,包括三目運(yùn)算符和錯誤處理等方面,以及對變量命名的書寫建議,需要的朋友可以參考下
    2016-04-04
  • iOS內(nèi)存錯誤EXC_BAD_ACCESS的解決方法

    iOS內(nèi)存錯誤EXC_BAD_ACCESS的解決方法

    iOS開發(fā),最郁悶的莫過于程序毫無征兆地就崩潰了,用bt命令打出調(diào)用棧,給出的是一堆系統(tǒng)EXC_BAD_ACCESS的信息,根本沒辦法定位問題出現(xiàn)在哪里
    2013-06-06
  • iOS開發(fā)之UIScrollView詳解

    iOS開發(fā)之UIScrollView詳解

    UIScrollView使用非常廣,本文研究UIScrollView各屬性和方法,明白它們的意義、作用。這里我們整理UIScrollView一些常見用法以及一些效果的實(shí)現(xiàn)思路。
    2016-04-04
  • 解析iOS開發(fā)中的FirstResponder第一響應(yīng)對象

    解析iOS開發(fā)中的FirstResponder第一響應(yīng)對象

    這篇文章主要介紹了解析iOS開發(fā)中的FirstResponder第一響應(yīng)對象,包括View的FirstResponder的釋放問題,需要的朋友可以參考下
    2015-10-10
  • iOS簡單登錄LoginViewController、注冊RegisterViewController等功能實(shí)現(xiàn)方法

    iOS簡單登錄LoginViewController、注冊RegisterViewController等功能實(shí)現(xiàn)方法

    這篇文章主要為大家詳細(xì)介紹了iOS簡單登錄LoginViewController、注冊RegisterViewController、UcenterViewController功能實(shí)現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • IOS 開發(fā)之NSURL基本操作

    IOS 開發(fā)之NSURL基本操作

    這篇文章主要介紹了IOS 開發(fā)之NSURL基本操作的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • IOS圖片設(shè)置毛玻璃效果

    IOS圖片設(shè)置毛玻璃效果

    這篇文章主要介紹了IOS圖片設(shè)置毛玻璃效果的相關(guān)資料,需要的朋友可以參考下
    2016-03-03

最新評論