iOS開發UIPickerView常用屬性方法

來源:互聯網
上載者:User

標籤:ack   清單項目   ttext   imp   load   skin   方式   sts   setfont   

//

//  ViewController.m

//  UIPickerViewAll

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

/*

 UIPickView控制項常用的方法和屬性:

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

 (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

 

iOS開發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.