01-UIPickerView

一.UIPickerView

1.UIPickerView的常見屬性

// 數(shù)據(jù)源(用來告訴UIPickerView有多少列多少行)
@property(nonatomic,assign) id<UIPickerViewDataSource> dataSource;

// 代理(用來告訴UIPickerView每1列的每1行顯示什么內(nèi)容,監(jiān)聽UIPickerView的選擇)
@property(nonatomic,assign) id<UIPickerViewDelegate>   delegate;

// 是否要顯示選中的指示器
@property(nonatomic) BOOL showsSelectionIndicator;

// 一共有多少列
@property(nonatomic,readonly) NSInteger numberOfComponents;

2.UIPickerView的常見方法

// 重新刷新所有列
- (void)reloadAllComponents;

// 重新刷新第component列
- (void)reloadComponent:(NSInteger)component;

// 主動(dòng)選中第component列的第row行
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;

// 獲得第component列的當(dāng)前選中的行號(hào)
- (NSInteger)selectedRowInComponent:(NSInteger)component;

3.數(shù)據(jù)源方法(UIPickerViewDataSource)

//  一共有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;

//  第component列一共有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

4.代理方法(UIPickerViewDelegate)

//  第component列的寬度是多少
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;

//  第component列的行高是多少
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;

//  第component列第row行顯示什么文字
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

//  第component列第row行顯示怎樣的view(內(nèi)容)
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;

//  選中了pickerView的第component列第row行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;

二.UIDatePicker

1.常見屬性

// datePicker的顯示模式
@property (nonatomic) UIDatePickerMode datePickerMode;

// 顯示的區(qū)域語言
@property (nonatomic, retain) NSLocale   *locale;

2.監(jiān)聽UIDatePicker的選擇

  • 因?yàn)閁IDatePicker繼承自UIControl,所以通過addTarget:...監(jiān)聽

三.Demo

1.UIPickerView的demo(點(diǎn)菜)

效果圖:

1.png

需求:選擇器中選擇的什么遵蚜,相應(yīng)的下面的label就顯示什么局装。

代碼:(若想要demo示例副渴,可私下聯(lián)系)

#import "ViewController.h"
@interface ViewController ()<UIPickerViewDataSource, UIPickerViewDelegate>
// pickView
@property (weak, nonatomic) IBOutlet UIPickerView *pickView;
// 所有食物
@property (nonatomic, strong) NSArray *foodArray;
// 水果
@property (weak, nonatomic) IBOutlet UILabel *fruitLabel;
// 主食
@property (weak, nonatomic) IBOutlet UILabel *foodLabel;
// 飲料
@property (weak, nonatomic) IBOutlet UILabel *drinkLabel;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 設(shè)置默認(rèn)選中的內(nèi)容
    self.fruitLabel.text = self.foodArray[0][0];
    self.foodLabel.text = self.foodArray[1][0];
    self.drinkLabel.text = self.foodArray[2][0];
}

#pragma mark - 懶加載
-(NSArray *)foodArray
{
    if (_foodArray == nil) {
        // 加載plist文件
        NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"foods.plist" ofType:nil];
        // 初始化(分配內(nèi)存空間)
        _foodArray = [NSArray arrayWithContentsOfFile:fullPath];
    }
    return _foodArray;
}
    
#pragma mark - UIPickerViewDataSource
// pickview一共多少列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return self.foodArray.count;
}
// 返回pickerView的第component列有多少行
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    // 獲取對(duì)應(yīng)列的數(shù)組
    NSArray *subFoods = self.foodArray[component];
    // 獲取對(duì)應(yīng)列的行數(shù)
    return subFoods.count;
}

#pragma mark - UIPickerViewDelegate
// 返回第component列的第row行顯示什么內(nèi)容
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    // 獲取對(duì)應(yīng)列的數(shù)組
    NSArray *subFoods = self.foodArray[component];
    // 獲取對(duì)應(yīng)行的標(biāo)題
    NSString *title = subFoods[row];
    return title;
}
    
// 當(dāng)選中了pickerView的某一行的時(shí)候調(diào)用
// 會(huì)將選中的列號(hào)和行號(hào)作為參數(shù)傳入
// 只有通過手指選中某一行的時(shí)候才會(huì)調(diào)用
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    // 獲取對(duì)應(yīng)列和行的數(shù)據(jù)
    NSString *title = self.foodArray[component][row];
    // 判斷選擇的是哪一列播赁,根據(jù)列號(hào)設(shè)置對(duì)應(yīng)的數(shù)據(jù)
    if (component == 0) {
        self.fruitLabel.text = title;
    } else if (component == 1) {
        self.foodLabel.text = title;
    } else {
        self.drinkLabel.text = title;
    }
}

@end

可能出現(xiàn)的問題:

  1. 數(shù)據(jù)顯示不出來赁还?
    原因:
  • 查看一下是否設(shè)置了代理或則數(shù)據(jù)源(或則是否進(jìn)行了連線)


    2.png
  • 查看一下設(shè)置列數(shù)時(shí)是不是用的是點(diǎn)語法self.foodArray.count;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末棚饵,一起剝皮案震驚了整個(gè)濱河市辆苔,隨后出現(xiàn)的幾起案子楼镐,更是在濱河造成了極大的恐慌喷橙,老刑警劉巖啥么,帶你破解...
    沈念sama閱讀 216,843評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異贰逾,居然都是意外死亡悬荣,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,538評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門疙剑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來氯迂,“玉大人,你說我怎么就攤上這事言缤〗朗矗” “怎么了?”我有些...
    開封第一講書人閱讀 163,187評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵管挟,是天一觀的道長轿曙。 經(jīng)常有香客問我,道長僻孝,這世上最難降的妖魔是什么拳芙? 我笑而不...
    開封第一講書人閱讀 58,264評(píng)論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮皮璧,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘分飞。我一直安慰自己悴务,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,289評(píng)論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著讯檐,像睡著了一般羡疗。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上别洪,一...
    開封第一講書人閱讀 51,231評(píng)論 1 299
  • 那天叨恨,我揣著相機(jī)與錄音,去河邊找鬼挖垛。 笑死痒钝,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的痢毒。 我是一名探鬼主播送矩,決...
    沈念sama閱讀 40,116評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼哪替!你這毒婦竟也來了栋荸?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,945評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤凭舶,失蹤者是張志新(化名)和其女友劉穎晌块,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體帅霜,經(jīng)...
    沈念sama閱讀 45,367評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡匆背,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,581評(píng)論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了义屏。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片靠汁。...
    茶點(diǎn)故事閱讀 39,754評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖闽铐,靈堂內(nèi)的尸體忽然破棺而出蝶怔,到底是詐尸還是另有隱情,我是刑警寧澤兄墅,帶...
    沈念sama閱讀 35,458評(píng)論 5 344
  • 正文 年R本政府宣布踢星,位于F島的核電站,受9級(jí)特大地震影響隙咸,放射性物質(zhì)發(fā)生泄漏沐悦。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,068評(píng)論 3 327
  • 文/蒙蒙 一五督、第九天 我趴在偏房一處隱蔽的房頂上張望藏否。 院中可真熱鬧,春花似錦充包、人聲如沸副签。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,692評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽淆储。三九已至冠场,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間本砰,已是汗流浹背碴裙。 一陣腳步聲響...
    開封第一講書人閱讀 32,842評(píng)論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留点额,地道東北人舔株。 一個(gè)月前我還...
    沈念sama閱讀 47,797評(píng)論 2 369
  • 正文 我出身青樓,卻偏偏與公主長得像咖楣,于是被迫代替她去往敵國和親督笆。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,654評(píng)論 2 354

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