UIPickView

1.搭建界面

  1> 注意點(diǎn):UIPickerView的高度不能改,默認(rèn)162,PickerView里面每行的高度可以改,不要弄混淆了。

2.UIPickerView

1> 如何使用UIPickerView展示數(shù)據(jù)?
進(jìn)入U(xiǎn)IPickerView頭文件,有數(shù)據(jù)源和代理,聯(lián)想到UITableView,模仿UITableView的用法缘揪。
2> 讓控制器作為PickerView的數(shù)據(jù)源,控制器遵守PickerView的數(shù)據(jù)源方法
3> PickerView的數(shù)據(jù)源方法

1> numberOfComponentsInPickerView: 返回多少列
2> pickerView:numberOfRowsInComponent: 返回第component列有多少行
3> 和UITableView的區(qū)別,每一行長什么樣,是由PickerView的代理決定的密浑。

4> PickerView的代理方法

(1)  返回第component列第row行長什么樣迷郑。
第component列第row行的展示標(biāo)題
- (NSString *)pickerView:(UIPickerView *)pickerViewtitleForRow:(NSInteger)row forComponent:(NSInteger)component
第component列第row行帶屬性的標(biāo)題
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
第component列第row行展示的視圖
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)components usingView:(UIView *)view;
(2) 返回第component列每一行的高度和寬度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;
(3) 選中第component列第row行調(diào)用
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;

5>記錄當(dāng)前選中的行號

  _proIndex = [pickerView selectedRowInComponent:0];

3.UIPickerView的應(yīng)用

1汗捡、單個(gè)PickerView的使用

//PickerView是用來展示數(shù)據(jù)的組件

#import "ViewController.h"

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
//遵循協(xié)議
@property (nonatomic,strong)UIPickerView * pickerView;//自定義pickerview
@property (nonatomic,strong)NSArray * letter;//保存要展示的字母
@property (nonatomic,strong)NSArray * number;//保存要展示的數(shù)字

@end

@implementation ViewController

- (void)viewDidLoad
 {
    [super viewDidLoad];

    //獲取需要展示的數(shù)據(jù)
    [self loadData];

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

    //指定數(shù)據(jù)源和委托
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
}
#pragma mark 加載數(shù)據(jù)
-(void)loadData
{
    //需要展示的數(shù)據(jù)以數(shù)組的形式保存
    self.letter = @[@"aaa",@"bbb",@"ccc",@"ddd"];
    self.number = @[@"111",@"222",@"333",@"444"];
}

#pragma mark UIPickerView DataSource Method 數(shù)據(jù)源方法

//指定pickerview有幾個(gè)表盤
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;//第一個(gè)展示字母、第二個(gè)展示數(shù)字
}

//指定每個(gè)表盤上有幾行數(shù)據(jù)
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSInteger result = 0;
    switch (component) {
        case 0:
            result = self.letter.count;//根據(jù)數(shù)組的元素個(gè)數(shù)返回幾行數(shù)據(jù)
            break;
        case 1:
            result = self.number.count;
            break;

        default:
            break;
    }

    return result;
}

#pragma mark UIPickerView Delegate Method 代理方法

//指定每行如何展示數(shù)據(jù)(此處和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;
}

2.兩個(gè)PickerView聯(lián)動(dòng)
coding地址:https://coding.net/u/Panzz/p/UIPickerViewDemo/git

#import "T2ViewController.h"

@interface T2ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (strong, nonatomic) IBOutlet UIPickerView *pickerView;
@property (nonatomic,strong)NSArray * provinces;//展示省份
@property (nonatomic,strong)NSArray * cities;//展示城市
@property (nonatomic,strong)UILabel * label;

@end

@implementation T2ViewController

- (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"]];
    
    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];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    NSInteger rows = 0;
    switch (component) {
        case 0:
            rows = self.provinces.count;
            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;
}

//選中時(shí)回調(diào)的委托方法,在此方法中實(shí)現(xiàn)省份和城市間的聯(lián)動(dòng)
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    switch (component) {
        case 0://選中省份表盤時(shí)蒙兰,根據(jù)row的值改變城市數(shù)組,刷新城市數(shù)組
            self.cities = self.provinces[row][@"Cities"];
            [self.pickerView reloadComponent:1];
            break;
        case 1:
            //            NSLog(@"當(dāng)前選擇目的地為:%@%@",self.provinces[[self.pickerView selectedRowInComponent:0]][@"State"],self.cities[[self.pickerView selectedRowInComponent:1]][@"city"]);
            self.label.text = [NSString stringWithFormat:@"%@%@",self.provinces[[self.pickerView selectedRowInComponent:0]][@"State"],self.cities[[self.pickerView selectedRowInComponent:1]][@"city"]];
            break;
            
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

3.自定義pickerview展示數(shù)據(jù)的方式label

#pragma mark pickerView Method
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;//表盤數(shù)量
}

//判斷是哪個(gè)pickerview,返回相應(yīng)的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;
}

//判斷是哪個(gè)pickerview搜变,返回相應(yīng)的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設(shè)置字體大小和顏色等
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    //可以通過自定義label達(dá)到自定義pickerview展示數(shù)據(jù)的方式
    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];//調(diào)用上一個(gè)委托方法采缚,獲得要展示的title
    return pickerLabel;
}
//選中某行后回調(diào)的方法,獲得選中結(jié)果
- (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];
    }
}

代碼地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末痹雅,一起剝皮案震驚了整個(gè)濱河市仰担,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌绩社,老刑警劉巖摔蓝,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異愉耙,居然都是意外死亡贮尉,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進(jìn)店門朴沿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來猜谚,“玉大人,你說我怎么就攤上這事赌渣∥呵Γ” “怎么了?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵坚芜,是天一觀的道長览芳。 經(jīng)常有香客問我,道長鸿竖,這世上最難降的妖魔是什么沧竟? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮缚忧,結(jié)果婚禮上悟泵,老公的妹妹穿的比我還像新娘。我一直安慰自己闪水,他們只是感情好糕非,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著球榆,像睡著了一般朽肥。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上芜果,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天鞠呈,我揣著相機(jī)與錄音,去河邊找鬼右钾。 笑死蚁吝,一個(gè)胖子當(dāng)著我的面吹牛旱爆,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播窘茁,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼怀伦,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了山林?” 一聲冷哼從身側(cè)響起房待,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎驼抹,沒想到半個(gè)月后桑孩,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡框冀,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年流椒,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片明也。...
    茶點(diǎn)故事閱讀 39,769評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡宣虾,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出温数,到底是詐尸還是另有隱情绣硝,我是刑警寧澤,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布撑刺,位于F島的核電站鹉胖,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏猜煮。R本人自食惡果不足惜次员,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一败许、第九天 我趴在偏房一處隱蔽的房頂上張望王带。 院中可真熱鬧,春花似錦市殷、人聲如沸愕撰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽搞挣。三九已至,卻和暖如春音羞,著一層夾襖步出監(jiān)牢的瞬間囱桨,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工嗅绰, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留舍肠,地道東北人搀继。 一個(gè)月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像翠语,于是被迫代替她去往敵國和親叽躯。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 一肌括、介紹UIPickView和UIDatePicker 1.UIPickView什么時(shí)候用? ?通常在注冊模塊,當(dāng)...
    Hevin_Chen閱讀 1,139評論 0 2
  • 廢話不多說点骑,直接上干貨 ---------------------------------------------...
    小小趙紙農(nóng)閱讀 3,352評論 0 15
  • 首先看一個(gè)時(shí)間選擇器 本時(shí)間選擇器是建立在彈出視圖上的,也可以在普通視圖上顯示谍夭。 定義一個(gè)彈出框(具體的彈出框功能...
    光_遙遠(yuǎn)閱讀 246評論 0 0
  • 一 :預(yù)期效果 :點(diǎn)擊按鈕出現(xiàn) UIPickerView 背景變?yōu)榘胪该餍Ч?.1 在按鈕的點(diǎn)擊事件中 //創(chuàng)建全...
    lalala1112389閱讀 483評論 0 0
  • 生活 周一(12.5)上午就給煙草所甲方遠(yuǎn)程發(fā)布了一個(gè)新版本并郵件通知了甲方黑滴,晚上去圖書館還書。完成11月的月記紧索,...
    im天行閱讀 142評論 0 0