實例講解iOS應(yīng)用開發(fā)中UIPickerView滾動選擇欄的用法
基礎(chǔ)
1.UIPickerView 屬性
數(shù)據(jù)源(用來告訴UIPickerView有多少列多少行)
@property(nonatomic,assign) id dataSource;
代理(用來告訴UIPickerView每1列的每1行顯示什么內(nèi)容,監(jiān)聽UIPickerView的選擇)
@property(nonatomic,assign) id delegate;
是否要顯示選中的指示器
@property(nonatomic) BOOL showsSelectionIndicator;
一共有多少列
@property(nonatomic,readonly) NSInteger numberOfComponents;
2.UIPickerView方法
重新刷新所有列
- (void)reloadAllComponents;
重新刷新第component列
- (void)reloadComponent:(NSInteger)component;
主動選中第component列的第row行
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;
獲得第component列的當前選中的行號
- (NSInteger)selectedRowInComponent:(NSInteger)component;
3.UIPickerView數(shù)據(jù)源方法
一共有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
第component列一共有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
4.UIPickerView代理方法
第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;
實例
UIPickerView 作為iOS的一個常用控件相信大家都有這方面的需求。
今天我們就簡單創(chuàng)建一個:
新建項目 命名:TestUIPickerView
在默認生成的ViewController中創(chuàng)建UIPickerView
首先在viewDidLoad 的方法中創(chuàng)建
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 選擇框
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 100, 320, 216)];
// 顯示選中框
pickerView.showsSelectionIndicator=YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[self.view addSubview:pickerView];
_proTimeList = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",nil];
_proTitleList = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",nil];
}
然后,我們創(chuàng)建相關(guān)的代理方法
UIPickerViewDataSource 相關(guān)代理
#pragma Mark -- UIPickerViewDataSource
// pickerView 列數(shù)
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
// pickerView 每列個數(shù)
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 0) {
return [_proTitleList count];
}
return [_proTimeList count];
}
UIPickerViewDelegate 相關(guān)代理方法
#pragma Mark -- UIPickerViewDelegate
// 每列寬度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
if (component == 1) {
return 40;
}
return 180;
}
// 返回選中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component == 0) {
NSString *_proNameStr = [_proTitleList objectAtIndex:row];
NSLog(@"nameStr=%@",_proNameStr);
} else {
NSString *_proTimeStr = [_proTimeList objectAtIndex:row];
NSLog(@"_proTimeStr=%@",_proTimeStr);
}
}
//返回當前行的內(nèi)容,此處是將數(shù)組中數(shù)值添加到滾動的那個顯示欄上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0) {
return [_proTitleList objectAtIndex:row];
} else {
return [_proTimeList objectAtIndex:row];
}
}
完成以上代碼之后 我們就可以運行項目查看效果
如下圖:
相關(guān)文章
AVFoundation AVCaptureSession媒體捕捉
這篇文章主要為大家介紹了ios開發(fā)AVFoundation AVCaptureSession媒體捕捉詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10iOS利用UIScrollView實現(xiàn)圖片的縮放實例代碼
本篇文章主要介紹了iOS利用UIScrollView實現(xiàn)圖片的縮放實例代碼,具有一定的參考價值,有興趣的可以了解一下2017-07-07iOS 封裝導(dǎo)航欄及返回,獲取控件所在控制器的實例
下面小編就為大家分享一篇iOS 封裝導(dǎo)航欄及返回,獲取控件所在控制器的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01