您的位置:首页 > 产品设计 > UI/UE

UIPickerView

2016-06-14 15:53 423 查看
1. UIPickerView示例

选取器被用来取代PC上面的下拉菜单,它是一个大大的滚轮,它占用固定的大小 320×216.

2.  UIPickerView使用步骤

2.1   创建UIPickerView并加入到当前控制器中

_pickerView = [[UIPickerView
alloc] initWithFrame:CGRectMake(0,
0, 375,
216)];

 

[self.view
addSubview:_pickerView];
2.2        设置UIPickerView

//显示选中框
 _pickerView.showsSelectionIndicator =
YES;

还可以设置背景色等,因为它是一个view

2.3        指定UIPickerView的数据源

_pickerView.dataSource =
self;

2.4        实现数据源方法

(1)  创建显示需要的数据

NSArray *dataArray = [[NSArray
alloc]initWithObjects:@"许嵩",@"周杰伦",@"梁静茹",@"许飞",@"凤凰传奇",@"阿杜",@"方大同",@"林俊杰",@"胡夏",@"邱永传",
nil];
    _pickerViewData = dataArray;

(2)   实现数据源方法

//返回显示的列数,有几个选取器就返回几
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return
1;
}

 

//返回显示的行数
-(NSInteger)pickerView:(UIPickerView *)pickerViewnumberOfRowsInComponent:(NSInteger)component
{
    return
_pickerViewData.count;
}

2.5        指定UIPickerView的代理

_pickerView.delegate =
self;

2.6        实现代理方法

//显示当前的内容,此处是将数组中数值添加到滚动的那个数据栏上
// //
设置UIPickView每行显示的内容,类似于tableview
中设置每个单元格的函数
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return  [_pickerViewData
objectAtIndex:row];//每行显示_pickerViewData数组中对应位置的数据
}

3. 点击按钮显示信息

-(void) buttonPressed:(id)sender
{
//找到选中的行
NSInteger row =[_pickerView
selectedRowInComponent:0];//因为只有一个选取器,所以这里是0,类似tableview不分组时
//取出被选中行的内容
    NSString *selected = [_pickerViewData
objectAtIndex:row];
    NSString *message = [[NSString
alloc] initWithFormat:@"你选择的是:%@",selected];
    //将内容显示到UIAlertView中
    UIAlertView *alert = [[UIAlertView
alloc] initWithTitle:@"提示"
                                                   message:message
                                                  delegate:self
                                         
cancelButtonTitle:@"OK"
                                         
otherButtonTitles: nil];
    [alert show];
}
 

4.  UIPickerView的数据源方法(2个)

本协议仅有两个实例方法,均需要实现
(1)
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
作用:返回pickerView应该有几个component
 
(2)
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger) component
作用:返回指定component应该有几个row
 
5.  UIPickerView的代理方法

(1)作用:
当用户选择某个row时,picker view调用此函数
 
-(void) pickerView: (UIPickerView*)pickerView didSelectRow: (NSInteger)row inComponent: (NSInteger)component
 
(2)由picker view调用,当其在绘制row内容,需要row的高度时
-(CGFloat) pickerView:(UIPickerView*)pickerView rowHeightForComponent: (NSInteger) component
 
(3)当picker view需要给指定的component.row指定title时,调用此函数
-(NSString *)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger) component
 
(4)当picker view需要给指定的component.row指定view时,调用此函数.返回值为用作row内容的view
类似于tableview
中的设置headerview中的内容是一个view而不是文本的函数
-(UIView *)pickerView: (UIPickerView*)pickerView view ForRow:(NSInteger) row forComponent:(NSInteger) component reusingView:(UIView*) view
 
(5)当picker view
需要row的宽度时,调用此函数
- (CGFloat)pickerView: (UIPickerView *)pickerViewwidthForComponent:(NSInteger)
component
 
6.  其他实例方法
(1)
- (NSInteger)numberOfRowsInComponent:(NSInteger)component
参数为component的序号(从左到右,以0起始),返回指定的component中row的个数
(2)
-(void) reloadAllComponents
调用此方法使得PickerView向delegate: Query for new data forall components
(3)
-(void) reloadComponent: (NSInteger)component
参数为需更新的component的序号,调用此方法使得PickerView向其delegate:
Query for new data
(4)
-(CGSize) rowSizeForComponent: (NSInteger)component
参数为component的序号,返回值为the size of rows in
the givencomponents, picker view 通过调用委托方法中的pickerView:widthForComponent:和pickerView:rowHeightForComponent:获得返回值
(5)
-(NSInteger) selectedRowInComponent:(NSInteger) component
参数为component的序号,返回被选中row的序号,若无row被选中,则返回-1
(6)
-(void) selectRow: (NSInteger)rowinComponent: (NSInteger)component animated: (BOOL)animated
作用:在代码指定要选择的某component的某row
参数:row序号,component序号,BOOL值(若为YES,转动spin到你选择的新值,若为NO,直接显示你选择的值)
(7)
-(UIView *) viewForRow: (NSInteger)rowforComponent: (NSInteger)component
参数:row序号,component序号,返回由委托方法pickerView:viewForRow:forComponentreusingView:指定的view.如果委托对象并没有实现这个方法,或者说这个view并不是可见的,则返回nil
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS uipickerview