iOSUIPickerView使用
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>{
NSArray *_nameArray;
}
@property (strong, nonatomic) UIPickerView *pickerView;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 162)];
self.pickerView.backgroundColor = [UIColor whiteColor];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
[self.view addSubview:self.pickerView];
[self.pickerView reloadAllComponents];//刷新UIPickerView
_nameArray = [NSArray arrayWithObjects:@"北京",@"上海",@"广州",@"深圳",@"重庆",@"武汉",@"天津",nil];
}
#pragma mark pickerview function
//返回有几列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
//返回指定列的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component==0) {
return 5;
} else if(component==1){
return [_nameArray count];
}
return [_nameArray count];
}
//返回指定列,行的高度,就是自定义行的高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return 20.0f;
}
//返回指定列的宽度
- (CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
if (component==0) {//iOS6边框占10+10
return self.view.frame.size.width/3;
} else if(component==1){
return self.view.frame.size.width/3;
}
return self.view.frame.size.width/3;
}
// 自定义指定列的每行的视图,即指定列的每行的视图行为一致
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
if (!view){
view = [[UIView alloc]init];
}
UILabel *text = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width/3, 20)];
text.textAlignment = NSTextAlignmentCenter;
text.text = [_nameArray objectAtIndex:row];
[view addSubview:text];
//隐藏上下直线
[self.pickerView.subviews objectAtIndex:1].backgroundColor = [UIColor clearColor];
[self.pickerView.subviews objectAtIndex:2].backgroundColor = [UIColor clearColor];
return view;
}
//显示的标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSString *str = [_nameArray objectAtIndex:row];
return str;
}
//显示的标题字体、颜色等属性
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSString *str = [_nameArray objectAtIndex:row];
NSMutableAttributedString *AttributedString = [[NSMutableAttributedString alloc]initWithString:str];
[AttributedString addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18], NSForegroundColorAttributeName:[UIColor whiteColor]} range:NSMakeRange(0, [AttributedString length])];
return AttributedString;
}//NS_AVAILABLE_IOS(6_0);
//被选择的行
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSLog(@"HANG%@",[_nameArray objectAtIndex:row]);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
效果如下:

iOSUIPickerView使用的更多相关文章
- ios-UIPickerView基本使用
#import "ViewController.h" @interface ViewController ()<UIPickerViewDataSource,UIPicker ...
- iOS- UIPickerView餐厅点餐系统
在餐厅里的点餐系统的核心控件就是UIPickerView 今天晚上在整理以前的项目笔记时,特意把UIPickerView单独拿出来,做了一个简陋的点餐道具. 因为没有素材图片,所有大家将就看看吧 0. ...
- webapp通用选择器:iosselect
1,这个组件解决什么问题 在IOS系统中,safari浏览器的select标签默认展示样式和iOS-UIPickerView展示方式一致,形如下图: 这个选择器操作方便,样式优美.但是在安卓系统中展示 ...
随机推荐
- FluentScheduler
The job configuration is handled in a Registry class. A job is either an Action or a class that inhe ...
- 【struts2】Struts2的运行流程
1)前提条件 在讲解流程之前,假设我们已经建立了的一个名为strutsDeepen的web工程,该工程仅仅实现了简单的用户登陆与欢迎界面.具体的实现为: 在web.xml中配置了Struts2的过滤器 ...
- Oracle 12C -- Plug in a Non-CDB as a PDB
1.备份non-CDB数据库2.关闭non-CDB数据库 SQL> shutdown immediate; 3.将non-CDB至于只读状态 SQL> startup open read ...
- 【转】编辑器与IDE
编辑器与IDE 无谓的编辑器战争 很多人都喜欢争论哪个编辑器是最好的.其中最大的争论莫过于 Emacs 与 vi 之争.vi 的支持者喜欢说:“看 vi 打起字来多快,手指完全不离键盘,连方向键都可以 ...
- Android Framework中的线程Thread及它的threadLoop方法
当初跟踪Camera的代码中的时候一直追到了HAL层,而在Framework中的代码看见了许很多多的Thread.它们普遍的特点就是有一个threadLoop方法.依照字面的意思应该是这个线程能够循环 ...
- 基于TransactionScope类的分布式隐式事务
System.Transactions 命名空间中除了上一节中提到的基于 Transaction 类的显式编程模型,还提供使用 TransactionScope 类的隐式编程模型,它与显示编程模型相比 ...
- xocodebulid 自动化打包 解决提示 ld: library not found for -lPods 问题
如果你的项目用到cocopod 第三方库.使用xcodebulid 估计会出现 ld: library not found for -lPods 以下 是我的解决办法 xcodebuild -work ...
- Android 开发工具介绍-SDK工具和平台工具
原文链接:http://android.eoe.cn/topic/android_sdk Android的SDK提供各种工具可以帮你为Android平台开发移动应用程序.这些工具被分类成两组:SDK工 ...
- andorid 直接解压后的xml的解密
1.首先可以去看看这个gitHub: https://github.com/tracer0tong/axmlprinter 2.把apk.py 和 axmlprinter.py下载下来. 2.1(如果 ...
- Android中的MVP架构分解和实现
1.概述 传统的Android开发架构通常是MVC模式. Model:业务逻辑和实体模型 View:相应于布局文件 Controllor:相应于Activity 单独从逻辑看起来很好,与我们做Web开 ...