ios-UIPickerView基本使用
2024-10-15 11:24:14
#import "ViewController.h" @interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
{
NSArray *pickerArray;
}
@property (weak, nonatomic) IBOutlet UIPickerView *myPickerView; @end @implementation ViewController - (void)viewDidLoad
{
[super viewDidLoad];
_myPickerView.dataSource=self;
_myPickerView.delegate=self;
_myPickerView.showsSelectionIndicator=YES;
pickerArray=[NSArray arrayWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"",@"", nil]; } - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return ;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return pickerArray.count;
} -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
CGRect rect=CGRectMake(, , [self pickerView:pickerView widthForComponent:row], [self pickerView:pickerView rowHeightForComponent:row]);
UIView *testView=[[UIView alloc]initWithFrame:rect];
[testView setBackgroundColor:[UIColor clearColor]];
[testView setOpaque:YES];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(, , [self pickerView:pickerView widthForComponent:row]-16.0f, [self pickerView:pickerView rowHeightForComponent:row])];
[label setBackgroundColor:[UIColor clearColor]];
label.textAlignment=NSTextAlignmentCenter;
label.text=pickerArray[row];
switch (row)
{
case :
case :
{
testView.backgroundColor=component==?[UIColor greenColor]:[UIColor blueColor];
}
case :
{
testView.backgroundColor=component==?[UIColor brownColor]:[UIColor redColor];
}
break;
default:
{
testView.backgroundColor=component==?[UIColor grayColor]:[UIColor orangeColor];
}
break;
}
label.font=[UIFont boldSystemFontOfSize:14.0f];
[testView addSubview:label];
return testView;
} //可有可无
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return pickerArray[row];
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
{
return ;
}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return ;
} - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{ NSLog(@"row=%ld",row);
}
最新文章
- C# BlockCollection
- Win10无法安装提示磁盘布局不受UEFI固件支持怎样解决
- Uber优步宁波司机注册正式开始啦! UBER宁波司机注册指南!
- 一次领域驱动设计(DDD)的实际应用
- iOS 设计模式之工厂模式
- UVa401 回文词
- 使用Yeoman搭建 AngularJS 应用 (3) —— 让我们搭建一个网页应用
- Http请求头中的字段理解
- font awesome使用笔记
- vijos 1942 [AH 2005] 小岛
- Webpack 4教程 - 第六部分 增强开发时体验
- cocos图片的选择以及压缩
- 更改Oracle字符集避免乱码
- fk输入地壳模型容易出错的地方
- strcmp用法
- tomcat下安装jenkins
- 1415. [NOIP2001]数的计数
- npm 安装 不快的解决办法
- 20145314郑凯杰 《Java程序设计》课程总结
- Redis 的安装配置介绍
热门文章
- 解决getOutputStream() has already been called for this response
- gdb之watch命令
- Java反射机制(Class类的使用)
- oracle学习 七 拼接变量+日期函数(持续更)
- ASP.NET的分页方法(四)
- HDU 1561The more, The Better(树形DP)
- POJ3974 Palindrome (manacher算法)
- 联想硬盘分区表格式修改 GPT ->; MBR
- JavaScript要点(十七) Math 对象
- js 基于函数伪造的方式实现继承