IOS第11天(3:UIPickerView省市聯動)

來源:互聯網
上載者:User

標籤:

*********

#import "ViewController.h"#import "Province.h"@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>@property (nonatomic,strong)NSArray *provinces;@property (nonatomic,assign)NSInteger indexOfProvice;//當前預設選中的省份@end@implementation ViewController-(NSArray *)provinces{    if (!_provinces) {        _provinces = [Province provinceList];    }        return _provinces;}- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    NSLog(@"%@",self.provinces);}// returns the number of ‘columns‘ to display.- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{    return 2;}// returns the # of rows in each component..- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{        if (component == 0) {//省分        return self.provinces.count;    }        //擷取對應省份的城市個數    Province *province = self.provinces[self.indexOfProvice];        return province.cities.count;}#pragma mark 顯示資料-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{                if(component == 0){//顯示省份的名字        //對應列行的省份        Province *province = self.provinces[row];        return province.name;    }        //擷取選中的城市,顯示城市名字    Province *selectedProvice = self.provinces[self.indexOfProvice];    return selectedProvice.cities[row];}//-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{//    //    UILabel *label = (UILabel *)view;//    if (!label) {//        label = [[UILabel alloc] init];//    }//    //    if(component == 0){//顯示省份的名字//        //對應列行的省份//        Province *province = self.provinces[row];//        label.text =  province.name;//        label.backgroundColor = [UIColor grayColor];//    }else{//        //        //擷取選中的城市,顯示城市名字//        Province *selectedProvice = self.provinces[self.indexOfProvice];//        label.text = selectedProvice.cities[row];//        label.backgroundColor = [UIColor blueColor];//    }//    //    //    //    //    return label;//}#pragma mark 選中行-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{        if (component == 0) {//第一列省的選中改變後,就要更新第二列資料        //更新選中省份的索引        self.indexOfProvice = row;                //重新整理資料        //全部重新整理        //[pickerView reloadAllComponents];                //部份重新整理        [pickerView reloadComponent:1];                //不管之前第二列選中第幾行,重新重新整理資料後,都顯示每二列的第一行        [pickerView selectRow:0 inComponent:1 animated:YES];    }}#pragma mark 設定寬度-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{    if (component == 0) {        return 80;    }        return 200;}@end

***model.m

#import "Province.h"@implementation Province-(instancetype)initWithDict:(NSDictionary *)dict{    if (self = [super init]) {        [self setValuesForKeysWithDictionary:dict];    }        return self;}+(instancetype)provinceWithDict:(NSDictionary *)dict{    return [[self alloc] initWithDict:dict];}+(NSArray *)provinceList{    //plist檔案路徑    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"provinces.plist" ofType:nil];    NSArray *provincePlist = [NSArray arrayWithContentsOfFile:filePath];        NSMutableArray *provinceM = [NSMutableArray array];    for (NSDictionary *dic in provincePlist) {        Province *prov = [Province provinceWithDict:dic];        [provinceM addObject:prov];    }        return provinceM;}@end

****model.h

#import <UIKit/UIKit.h>@interface Province : NSObject@property(nonatomic,copy)NSString *name;@property(nonatomic,strong)NSArray *cities;-(instancetype)initWithDict:(NSDictionary *)dict;+(instancetype)provinceWithDict:(NSDictionary *)dict;+(NSArray *)provinceList;@end

 

IOS第11天(3:UIPickerView省市聯動)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.