iOS開發UIPickerView經常使用屬性方法

//數組

//  ViewController.m佈局

//  UIPickerViewAll字體

#import "ViewController.h"動畫

 

@interface ViewController ()atom

 

@end代理

 

@implementation ViewControllercomponent

/*orm

 UIPickView控件經常使用的方法和屬性:ci

 (1)  - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; 返回PickerView的列數get

 (2)  - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

 返回PickView的component列對應的行數

 (3)  - (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

 返回每一列每一行的內容

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

 用戶選中PickView的某一列和某一行時會調用該方法

 (5)  - (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component

 修改PickView中component列row行的文本的樣式

 (6)  - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view 該方法返回的UIView的控件將直接做爲UIPickView對應的component 列row行的列表項

 (7)  - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component 設置component列對應的行高

 (8)  - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component

 該方法設置component列對應的寬度

 (9)  - (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated

 該方法設置選中的UIPickView 第component列row行項,最後一個參數animated表明是否要用到動畫

 (10)  @property(nonatomic,readonly) NSInteger numberOfComponents;

 獲取UIPickerView指定列中包含的列表項的數量,該屬性是隻讀的

 */

 

//簡單實用

//- (void)viewDidLoad

//{

//    [super viewDidLoad];

//

//    //獲取須要展現的數據

//    [self loadData];

//    

//    // 初始化pickerView

//    self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width, 200)];

//    [self.view addSubview:self.pickerView];

//    

//    //指定數據源和委託

//    self.pickerView.delegate = self;

//    self.pickerView.dataSource = self;

//}

//#pragma mark 加載數據

//-(void)loadData

//{

//    //須要展現的數據以數組的形式保存

//    self.letter = @[@"aaa",@"bbb",@"ccc",@"ddd"];

//    self.number = @[@"111",@"222",@"333",@"444"];

//}

//

//#pragma mark UIPickerView DataSource Method 數據源方法

//

////指定pickerview有幾個錶盤

//-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

//{

//    return 2;//第一個展現字母、第二個展現數字

//}

//

////指定每一個錶盤上有幾行數據

//-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

//{

//    NSInteger result = 0;

//    switch (component) {

//        case 0:

//            result = self.letter.count;//根據數組的元素個數返回幾行數據

//            break;

//        case 1:

//            result = self.number.count;

//            break;

//            

//        default:

//            break;

//    }

//    

//    return result;

//}

//

//#pragma mark UIPickerView Delegate Method 代理方法

//

////指定每行如何展現數據(此處和tableview相似)

//-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

//{

//    NSString * title = nil;

//    switch (component) {

//        case 0:

//            title = self.letter[row];

//            break;

//        case 1:

//            title = self.number[row];

//            break;

//        default:

//            break;

//    }

//    

//    return title;

//}

#pragma mark 兩個聯動

- (void)viewDidLoad

{

    [super viewDidLoad];

    

    //加載數據

    [self loadData];

    

    //指定委託

    self.pickerView.delegate = self;

    self.pickerView.dataSource = self;

}

 

//加載數據

-(void)loadData

{

    NSString * path = [[NSBundle mainBundle]pathForResource:@"area" ofType:@"plist"];

    self.provinces = [NSArray arrayWithContentsOfFile:path];

    self.cities = [NSArray arrayWithArray:self.provinces[0][@"Cities"]];

    

    //label的佈局約束

    self.label = [[UILabel alloc]initWithFrame:CGRectZero];

    self.label.translatesAutoresizingMaskIntoConstraints = NO;

    self.label.backgroundColor = [UIColor groupTableViewBackgroundColor];

    self.label.textColor = [UIColor greenColor];

    self.label.font = [UIFont systemFontOfSize:30];

    [self.label setTextAlignment:NSTextAlignmentCenter];

    [self.view addSubview:self.label];

    NSArray * labelTop = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[_pickerView]-30-[_label(50)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_pickerView,_label)];

    NSArray * labelH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[_label]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_label)];

    [self.view addConstraints:labelTop];

    [self.view addConstraints:labelH];

}

 

//有幾個錶盤(component)

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

    return 2;

}

 

//沒個錶盤有幾行數據(rows)

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

    NSInteger rows = 0;

    switch (component) {

        case 0:

            rows = self.provinces.count;//根據plist文件中的數據返回rows

            break;

        case 1:

            rows = self.cities.count;

            break;

        default:

            break;

    }

    return rows;

}

 

//每行的標題

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

    NSString * title = nil;

    switch (component) {

        case 0:

            title = self.provinces[row][@"State"];

            break;

        case 1:

            title = self.cities[row][@"city"];

            break;

        default:

            break;

    }

    return title;

}

 

//選中時回調的委託方法,在此方法中實現省份和城市間的聯動

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

{

    switch (component) {

        case 0://選中省份錶盤時,根據row的值改變城市數組,刷新城市數組,實現聯動

            self.cities = self.provinces[row][@"Cities"];

            [self.pickerView reloadComponent:1];

            break;

        case 1:

            self.label.text = [NSString stringWithFormat:@"%@%@",self.provinces[[self.pickerView selectedRowInComponent:0]][@"State"],self.cities[[self.pickerView selectedRowInComponent:1]][@"city"]];//若是選中第二個

            break;

            

        default:

            break;

    }

}

 

//修改pickerview的title樣式

//#pragma mark pickerView Method

//- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

//{

//    return 1;//錶盤數量

//}

//

////判斷是哪一個pickerview,返回相應的rows

//- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

//{

//    NSInteger rows = 0;

//    

//    if (pickerView == self.pickerView)

//    {

//        rows = self.pickerViewArr.count;

//    }

//    else

//    {

//        rows = self.pickerViewArr2.count;

//    }

//    

//    return rows;

//}

//

////判斷是哪一個pickerview,返回相應的title

//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

//{

//    NSString *str = nil;

//    

//    if (pickerView == self.pickerView)

//    {

//        str = self.pickerViewArr[row];

//    }

//    else

//    {

//        str = self.pickerViewArr2[row];

//    }

//    return str;

//}

//

//#pragma mark 給pickerview設置字體大小和顏色等

//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

//{

//    //能夠經過自定義label達到自定義pickerview展現數據的方式

//    UILabel* pickerLabel = (UILabel*)view;

//    

//    if (!pickerLabel)

//    {

//        pickerLabel = [[UILabel alloc] init];

//        pickerLabel.adjustsFontSizeToFitWidth = YES;

//        pickerLabel.textAlignment = NSTextAlignmentCenter;

//        [pickerLabel setBackgroundColor:[UIColor lightGrayColor]];

//        [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];

//    }

//    

//    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];//調用上一個委託方法,得到要展現的title

//    return pickerLabel;

//}

////選中某行後回調的方法,得到選中結果

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

//{

//    if (pickerView == self.pickerView)

//    {

//        self.pickerViewSelect = self.pickerViewArr[row];

//        NSLog(@"selected == %@",self.pickerViewSelect);

//    }

//    else

//    {

//        self.pickerViewSelect2 = self.pickerViewArr2[row];

//    }

//}

 

 

//轉自連接:http://www.jianshu.com/p/811882ba8d78

 

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

@end

相關文章
相關標籤/搜索