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
选取器被用来取代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
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法
- iOS App开发中的UISegmentedControl分段组件用法总结
- IOS开发环境windows化攻略
- iOS应用中UITableView左滑自定义选项及批量删除的实现
- iOS中UIAlertView警告框组件的使用教程
- 浅析iOS应用开发中线程间的通信与线程安全问题
- iOS中的UIKeyboard键盘视图使用方法小结
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法