UIPickerView選擇視圖

#import <UIKit/UIKit.h>

@interface VCRoot : UIViewController
<
//選擇視圖數據源協議
UIPickerViewDataSource,
//普通事件協議
UIPickerViewDelegate>
{
    
    //三組數據源,對應三個列表
    NSMutableArray* _arraySec01 ;
    NSMutableArray* _arraySec02 ;
    NSMutableArray* _arraySec03 ;
    NSInteger       _curIndex01 ;
    NSInteger       _curIndex02 ;
    NSInteger       _curIndex03 ;
}

@end

#define SCREEN_WIDTH                ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT               ([UIScreen mainScreen].bounds.size.height)
#import "VCRoot.h"
#import "UIColor+Category.h"
@interface VCRoot ()
@property (strong, nonatomic) UITextField *textFiled;//日期
@property (strong, nonatomic) UITextField *areaTextfield;//地址
@property (strong, nonatomic) NSMutableArray *provincesArray;



@end

@implementation VCRoot

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (UIColor *)colorWithRGBHex:(UInt32)hex
{
    int r = (hex >> 16) & 0xFF;
    int g = (hex >> 8) & 0xFF;
    int b = (hex) & 0xFF;
    
    return [UIColor colorWithRed:r / 255.0f
                           green:g / 255.0f
                            blue:b / 255.0f
                           alpha:1.0f];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor =  HEX_RGB(0xF7F7F7);

    //建立選擇視圖
    UIPickerView * pickerView = [[UIPickerView alloc] init] ;
    pickerView.tag = 1000;
    pickerView.backgroundColor = HEX_RGB(0x3D4DFE);
    //設置選擇視圖的位置
    pickerView.frame = CGRectMake(0, 20, self.view.frame.size.width , 200) ;
    //設置數據源代理
    pickerView.dataSource = self ;
    pickerView.delegate   = self ;
    
    [self.view addSubview:pickerView] ;
    [self.view addSubview:self.textFiled];
    [self.view addSubview:self.areaTextfield];
    //建立UIDatePicker(日期鍵盤)
    UIDatePicker *pick = [[UIDatePicker alloc] init];
    pick.tag = 1001;
    pick.datePickerMode = UIDatePickerModeDate; //ISO639語 編碼(中國zh -zhongwen)
    NSLocale *local = [NSLocale localeWithLocaleIdentifier:@"zh"];
    pick.locale = local;
    //UIDatePicker沒有代理方法
    //監聽UIDatePicker的值改變.
    [pick addTarget:self action:@selector(dateChange:)forControlEvents:UIControlEventValueChanged];
    // 定義鍵盤, 讓彈出的鍵盤是 個UIPickerView.( 定義的鍵盤是不須要設置尺寸的.)
    _textFiled.inputView = pick;
    
    //建立UIPickerView
    UIPickerView *pickV = [[UIPickerView alloc] init];
    //設置代理
    pickV.delegate = self;
    //設置數據源.
    pickV.dataSource = self;
    pickV.tag = 1002;
    //設置彈出鍵盤是一個UIPickerView
    _areaTextfield.inputView = pickV;
    
    
    
    
    //初始化數據
    _arraySec01 = [[NSMutableArray alloc] init] ;
    _arraySec02 = [[NSMutableArray alloc] init] ;
    _arraySec03 = [[NSMutableArray alloc] init] ;
    
    for (int i = 0 ; i < 100; i++)
    {
        NSString* str = [NSString stringWithFormat:@"第%d列",i+1] ;
        
        [_arraySec01 addObject:str] ;
        [_arraySec02 addObject:str] ;
        [_arraySec03 addObject:str] ;
    }
    
    _curIndex01 = -1 ;
    _curIndex02 = -1 ;
    _curIndex03 = -1 ;
}

#pragma mark 
-(void)dateChange:(UIDatePicker *)datePicker{
    //把日期轉成字符串.
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    //設置日期格式
    fmt.dateFormat = @"yyyy-MM-dd";
    //格式化日期.
    NSString *dateString = [fmt stringFromDate:datePicker.date];
    //給日期文本框賦值.
    _textFiled.text = dateString;

}
#pragma mark pickerView代理協議
//列數
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3 ;
}
//返回每列中有多少個元素
//參數一:選擇視圖
//參數二:列數
//列有多少行
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (pickerView.tag == 1000){
        //第一列元素個數
        if (component == 0)
        {
            return _arraySec01.count ;
        }
        //第二列的個數
        else if(component == 1)
        {
            return 100 ;
        }
        //第三列的個數
        else if (component == 2)
        {
            return _arraySec03.count ;
        }
    }else if (pickerView.tag == 1002){
        if (component == 0) {
            return self.provincesArray.count;
        }else{
            //第二組展有多少行,由當前選中的省份決定,當前選中哪一個省份.哪一個省份下的城市數組決定. 因此要先獲取當前先中的是哪一個省.
            //返回當前省份下城市的個數
            return 1;
        }
    }
    return 1 ;
}


//返回每一列的標題
//參數一:選擇視圖
//參數二:所在的行數
//參數三:所在的列索引
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString* strValue = nil ;
    if (pickerView.tag == 1000){
        if (component == 0)
        {
            strValue = _arraySec01[row] ;
            
        }
        else if (component == 1)
        {
            strValue = _arraySec02[row] ;
        }
        else
        {
            strValue = _arraySec03[row] ;
        }
    }else if (pickerView.tag == 1002){
    
        if (component == 0){
            strValue = _provincesArray[row];
        }
    }
    return strValue ;
}

//設置每行元素的高度
-(CGFloat) pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 40 ;
}

//設置每列的寬度
//根據列數來指定寬度
-(CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if (component == 0) {
        return 60 ;
    }
    else if (component == 1)
    {
        return 60 ;
    }
    return 60 ;
}


//返回視圖做爲元素內容顯示在控件
-(UIView*) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    //以前沒有建立(不能複用)視圖
    //新建立視圖對象
    if (pickerView.tag == 1000){
        if (view == nil)
        {
            UIImageView* iView = [[UIImageView alloc] init] ;
            
            int random = row % 7 + 1 ;
            
            NSString* strName = [NSString stringWithFormat:@"%d.png",random] ;
            
            UIImage* image = [UIImage imageNamed:strName] ;
            
            iView.image = image ;
            
            return iView ;
        }
        else
        {
            UIImageView* iView = (UIImageView*)view ;
            
            int random = row % 7 + 1 ;
            
            NSString* strName = [NSString stringWithFormat:@"%d.png",random] ;
            
            iView.image = [UIImage imageNamed:strName] ;
            
            return iView ;
        }
    }else if (pickerView.tag == 1002){
    
        return nil;
    }else{
    
        return nil;
    }
}

//當選擇視圖選中或中止時,調用此協議函數  component列  row行
-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSLog(@"當前選中的元素的列在%ld,行%ld",component,row) ;
    
    if (component == 0) {
        _curIndex01 = row % 7 ;
    }
    else if (component == 1)
    {
        _curIndex02 = row % 7 ;
    }
    else if (component == 2)
    {
        _curIndex03 = row % 7 ;
    }
    
    if (_curIndex01 == _curIndex02&&
        _curIndex02 == _curIndex03) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"" message:@"You Win!!!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] ;
        
        [alert show] ;
    }
}
#pragma mark END

#pragma mark 懶加載
-(UITextField *)textFiled{
    if (!_textFiled){
        _textFiled = [[UITextField alloc]init];
        _textFiled.frame = CGRectMake(0, 20+200, SCREEN_WIDTH, 30);
        _textFiled.backgroundColor = HEX_RGB(0x7CEEC6);
    }
    return _textFiled;
}
-(UITextField *)areaTextfield{
    if (!_areaTextfield){
        _areaTextfield = [[UITextField alloc]init];
        _areaTextfield.frame = CGRectMake(0, 220+30, SCREEN_WIDTH, 30);
        _areaTextfield.backgroundColor = HEX_RGB(0xEAA333);
    }
    return _areaTextfield;
}
//獲取plist文件 地理
-(NSMutableArray *)provincesArray{
    if (!_provincesArray){
            //建立數組
        _provincesArray = [NSMutableArray array];
        //獲取plist文件的路徑
        NSString*filePath= [[NSBundle mainBundle]pathForResource:@"area.plist" ofType:nil];
        //從plist 文件當中加載數據
        NSArray *tempArray = [NSArray arrayWithContentsOfFile:filePath];
        [_provincesArray addObject: tempArray];;
        //遍歷數組當中的每個元素.每一個元素都是一個字典,把字典轉成模型
//        for (NSDictionary *dict in tempArray) {
//            //建立省份模型對象.快速的將字典轉成模型
//            NSObject*item= [ProvincesItemprovincesItemWithDict:dict]; //把轉好的對象保存到數組當中.
//            [_provincesArray addObject:item]; }
    }
    return _provincesArray;
}
@end

area.plisthtml

參考文章:http://www.cnblogs.com/zhoudaquan/p/5014078.html數組

相關文章
相關標籤/搜索