UIPickerView

目錄:
1栋猖、單個(gè)PickerView的使用
2肄程、兩個(gè)PickerView聯(lián)動(dòng)
3、PickerView標(biāo)題顯示自定義

1.單個(gè)pickerview的使用

coding地址:https://coding.net/u/Panzz/p/UIPickerViewDemo/git

//PickerView是用來(lái)展示數(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è)表盤(pán)
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;//第一個(gè)展示字母、第二個(gè)展示數(shù)字
}

//指定每個(gè)表盤(pán)上有幾行數(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;
}

簡(jiǎn)單的pickerview效果圖(兩個(gè)pickerview不聯(lián)動(dòng))

2.兩個(gè)PickerView聯(lián)動(dòng)

coding地址:https://coding.net/u/Panzz/p/UIPickerViewDemo/git

@interface T2ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>//遵循協(xié)議
@property (strong, nonatomic) IBOutlet UIPickerView *pickerView;

//storyboard中拖拽
@property (nonatomic,strong)NSArray * provinces;//展示省份
@property (nonatomic,strong)NSArray * cities;//展示城市
@property (nonatomic,strong)UILabel * label;//展示選中的結(jié)果

@end

@implementation T2ViewController

- (void)viewDidLoad 
{
    [super viewDidLoad];

    //加載數(shù)據(jù)
    [self loadData];

    //指定委托
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
}

//加載數(shù)據(jù)
-(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];
}

//有幾個(gè)表盤(pán)(component)
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

//沒(méi)個(gè)表盤(pán)有幾行數(shù)據(jù)(rows)
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    NSInteger rows = 0;
    switch (component) {
        case 0:
            rows = self.provinces.count;//根據(jù)plist文件中的數(shù)據(jù)返回rows
            break;
        case 1:
            rows = self.cities.count;
            break;
        default:
            break;
    }
    return rows;
}

//每行的標(biāo)題
-(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://選中省份表盤(pán)時(shí)衫嵌,根據(jù)row的值改變城市數(shù)組读宙,刷新城市數(shù)組,實(shí)現(xiàn)聯(lián)動(dòng)
            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"]];//如果選中第二個(gè)
            break;
            
        default:
            break;
    }
}
實(shí)現(xiàn)省市之間聯(lián)動(dòng)

3.設(shè)置PickerView展示的title樣式

下面介紹自定義pickerview展示的label方法
(自定義pickerview展示數(shù)據(jù)的方式label)

通過(guò)自定義返回的label達(dá)到自定義pickerview行數(shù)據(jù)展示方式
#pragma mark pickerView Method
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;//表盤(pán)數(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
{
    //可以通過(guò)自定義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)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末桦锄,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子蔫耽,更是在濱河造成了極大的恐慌结耀,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,084評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件针肥,死亡現(xiàn)場(chǎng)離奇詭異饼记,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)慰枕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,623評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén)具则,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人具帮,你說(shuō)我怎么就攤上這事博肋〉驼” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,450評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵匪凡,是天一觀的道長(zhǎng)膊畴。 經(jīng)常有香客問(wèn)我,道長(zhǎng)病游,這世上最難降的妖魔是什么唇跨? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,322評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮衬衬,結(jié)果婚禮上买猖,老公的妹妹穿的比我還像新娘。我一直安慰自己滋尉,他們只是感情好玉控,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,370評(píng)論 6 390
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著狮惜,像睡著了一般高诺。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上碾篡,一...
    開(kāi)封第一講書(shū)人閱讀 51,274評(píng)論 1 300
  • 那天虱而,我揣著相機(jī)與錄音,去河邊找鬼开泽。 笑死薛窥,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的眼姐。 我是一名探鬼主播,決...
    沈念sama閱讀 40,126評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼佩番,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼众旗!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起趟畏,我...
    開(kāi)封第一講書(shū)人閱讀 38,980評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤贡歧,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后赋秀,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體利朵,經(jīng)...
    沈念sama閱讀 45,414評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,599評(píng)論 3 334
  • 正文 我和宋清朗相戀三年猎莲,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了绍弟。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,773評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡著洼,死狀恐怖樟遣,靈堂內(nèi)的尸體忽然破棺而出而叼,到底是詐尸還是另有隱情,我是刑警寧澤豹悬,帶...
    沈念sama閱讀 35,470評(píng)論 5 344
  • 正文 年R本政府宣布葵陵,位于F島的核電站,受9級(jí)特大地震影響瞻佛,放射性物質(zhì)發(fā)生泄漏脱篙。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,080評(píng)論 3 327
  • 文/蒙蒙 一伤柄、第九天 我趴在偏房一處隱蔽的房頂上張望绊困。 院中可真熱鬧,春花似錦响迂、人聲如沸考抄。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,713評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)川梅。三九已至,卻和暖如春然遏,著一層夾襖步出監(jiān)牢的瞬間贫途,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,852評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工待侵, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留丢早,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,865評(píng)論 2 370
  • 正文 我出身青樓秧倾,卻偏偏與公主長(zhǎng)得像怨酝,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子那先,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,689評(píng)論 2 354

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

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,095評(píng)論 25 707
  • 一农猬、UIPickerView(拾取器)的使用 1、UIPickerView控件生成的表格可以提供滾動(dòng)的輪盤(pán) ...
    愛(ài)攝影的鏟屎官閱讀 1,415評(píng)論 0 1
  • 人生究竟是怎樣的售淡。 我生活在這個(gè)世界有了18年的時(shí)光斤葱,可是我現(xiàn)在該去干什么? 我的未來(lái)該何去何從揖闸,如果再過(guò)這樣碌碌...
    壞先生丶閱讀 293評(píng)論 1 4
  • 鄰座阿姨很熱情地和周邊的人都大致聊上了幾句揍堕,然后又突然湊近, "你是去廣州結(jié)婚嗎汤纸?" (難道就因?yàn)闊岬牟恍械奈覔Q了...
    向晚_b682閱讀 234評(píng)論 0 2