UIPickerView 選擇器

UIPickerView繼承自UIView,所以不能像UIControl樣綁定事件處理方法,所以UIPickerView的事件處理由其委托對象完成。

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

_myPicker = [[UIPickerView alloc] init];

//設置高亮顯示
_myPicker.showsSelectionIndicator = YES;

//數(shù)據(jù)源
_myPicker.dataSource = self;
//代理
_myPicker.delegate = self;

//放在正中間
_myPicker.center = self.view.center;

//顯示的時候是第幾個,索引是從0開始的,顯示第5個
[_myPicker selectRow:4 inComponent:0 animated:YES];
//第二列,也是第5個
[_myPicker selectRow:4 inComponent:1 animated:YES];

[self.view addSubview:_myPicker];

numberOfComponents:獲取指定列中的列表項的數(shù)量,只讀屬性
showsSelectionIndicato:是否顯示UIPickerView中的選中標記(以高亮背景作為選中標記)
-numberOfRowsInComponent: 獲取列的數(shù)量
-rowSizeForComponent: 獲取指定列中列表項的大小。返回CGSize對象
-selectRow:inComponent:animated:設置指定列的特定列表項。是否使用動畫

UIPickerViewDataSource :
控制包含多少列,每一列又包含多少列表項(也是是行)

//設置數(shù)據(jù)源方法,返回2表示有兩列

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView  *)pickerView
{
    return 2;
}

//設置每一列,有多少行
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    //先定義一個變量
    NSInteger result;
    //判斷
    if (component == 0) {
        //是第一列的時候,給5行
        result = 5;
    } else if (component == 1){
        //第二列,給10行
        result = 10;
    }
    //返回數(shù)值
    return result;
    
}




//設置協(xié)議方法

//每一列寬度
// returns width of column and height of row for each component.
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return 150;
}

//每一行顯示的數(shù)據(jù)
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    //返回的是NSString類型的數(shù)據(jù)
    //返回每一行,因為索引是從0開始,+1
    NSString *result = [NSString stringWithFormat:@"我是第%ld行",(long)row + 1];
    return result;
}
UIpickerView.gif

PS:話說這個大概可以和日期相結(jié)合吧,回頭試一下

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • UIPickerView 繼承了UIView 沒有繼承UIControl UIPickerView的時間處理由其委...
    nalis風閱讀 1,675評論 0 0
  • UIPickerView也是一個選擇器控件,它比UIDatePicker更加通用,它可以生成單列的選擇器,也可生成...
    小蘑菇2閱讀 3,724評論 3 5
  • 轉(zhuǎn)自:http://www.cocoachina.com/bbs/read.php?tid=182382 UIPi...
    愛喝農(nóng)藥de清涼閱讀 610評論 0 1
  • 前言:UIPickerView 是一個選擇器控件, 它可以生成單列的選擇器,也可生成多列的選擇器,而且開發(fā)者完全可...
    yymyb閱讀 738評論 0 1
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139

友情鏈接更多精彩內(nèi)容