iOS界面編程-UIPickerView

類的介紹

UIPickerView類實現了對象,稱爲選擇器視圖,使用一個轉輪或老虎機的比喻來顯示一個或多個值集。用戶通過旋轉輪子來選擇值,以便將所需的值與一個選擇指示器對應。
它可以生成單列的選擇器,也可生成多列的選擇器,而且開發者完全可以自定義選擇項的外觀,因此用法非常靈活。

相關屬性方法

屬性:
1.獲取UIPickerView組件的數量
@property(nonatomic, readonly) NSInteger numberOfComponents
2.獲取某個指定組件的行數
- numberOfRowsInComponent:
3.獲取某個指定組件的行的大小。
- rowSizeForComponent:
方法:
1.重新加載所有組件
- reloadAllComponents
2 重新加載某個特定的組件。
- reloadComponent:
3、在picker view指定的一個組件中,選擇一行
- selectRow:inComponent:animated:
4、返回指定組件指定行的序列
- selectedRowInComponent:
5、返回給定組件,給定行的視圖
- viewForRow:forComponent:
6delegate 屬性

7dataSource 屬性

8顯示選擇指示器
showsSelectionIndicator Property

相關例子

-(void)initPickerView{
   UIPickerView *pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(20, 100, self.view.bounds.size.width-40, 100)];
    pickView.delegate = self;
    pickView.dataSource = self;
    [self.view addSubview:pickView];

}

其中UIPickerViewDataSource必須實現的兩個代理方法爲:
#pragma  remark -UIPickerViewDataSource

// 設置顯示列數爲多少.這裏設置只顯示一行
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

// 設置每個組件(列)的行爲多少,這裏設置都爲10行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return 10;
}

#pragma  remark -UIPickerViewDelegate
// 設置每個組件的寬度爲多少。
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return 140.0;
}
//設置每個組件行高爲多少
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 40.0;
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return  [NSString stringWithFormat:@"test %ld",(long)row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSLog(@"用戶選擇了第%ld行",(long)row);
}
發佈了67 篇原創文章 · 獲贊 20 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章