UIPickerView的使用
来源:互联网 发布:永宏plc用什么编程电缆 编辑:程序博客网 时间:2024/04/28 23:50
一般在应用程序中可能遇到像城市选择风格的UI ,那这个就是用UIPickerView来实现的 ;
UIPickerView的使用跟UITableView的使用很类似,都需要实现delegat 和dataSource。 那接下来看一下具体的操作步骤:
1.初始化pickerView
UIPickerView *pickerView = [[UIPickerViewalloc]initWithFrame:CGRectMake(0,100,380,216)];
pickerView.showsSelectionIndicator =YES;
pickerView.delegate =self;
pickerView.dataSource =self;
// pickerView.backgroundColor = [UIColor redColor];
[self.viewaddSubview:pickerView];
NSString *path = [[NSBundlemainBundle] pathForResource:@"ProvincesAndCities"ofType:@"plist"];//此处的plist见附件
provinceArr = [NSArrayarrayWithContentsOfFile:path];
cityArr = [provinceArr[0]objectForKey:@"Cities"];
2.接下来实现代理和数据源方法
//列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
//行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component==0)
{
return provinceArr.count;
}
return cityArr.count;
}
//每列高度
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 60;
}
//每列宽度
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
if(component==1)
{
return 200;
}
return 200;
}
//选中某一行
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(component == 0)
{
cityArr = [[provinceArrobjectAtIndex:row] objectForKey:@"Cities"];
[pickerViewselectRow:0inComponent:1animated:YES];
[pickerViewreloadComponent:1];
}
}
//返回每列当前行的内容
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component == 0)
{
return [[provinceArrobjectAtIndex:row] objectForKey:@"State"];
}
else
{
return [[cityArrobjectAtIndex:row] objectForKey:@"city"];
}
}
3.此处还可以自定义每一列显示的样式
//自定义
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
if (view==nil)
{
view = [[UIViewalloc]init];
}
if(component == 0)
{
UILabel *la = [[UILabel alloc]initWithFrame:CGRectMake(10,20, 30, 30)];
la.textAlignment =NSTextAlignmentCenter;
la.text = [NSStringstringWithFormat:@"%ld",row];
[view addSubview:la];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50,0, 120, 60)];
label.backgroundColor = [UIColorredColor];
label.text = [[provinceArrobjectAtIndex:row] objectForKey:@"State"];
label.textAlignment =NSTextAlignmentCenter;
[view addSubview:label];
return view;
}
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10,0, 70, 60)];
label.text = [[cityArrobjectAtIndex:row] objectForKey:@"city"];
label.backgroundColor = [UIColorgreenColor];
label.textAlignment =NSTextAlignmentCenter;
[view addSubview:label];
return view;
}
4.还可以设置字体
-(NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *str = nil ;
if(component == 0)
{
str = [[provinceArrobjectAtIndex:row] objectForKey:@"State"];
}
else
{
str = [[cityArrobjectAtIndex:row] objectForKey:@"city"];
}
NSMutableAttributedString *attributeString = [[NSMutableAttributedStringalloc]initWithString:str];
[attributeString addAttributes:@{NSFontAttributeName:[UIFontboldSystemFontOfSize:10.0f],NSForegroundColorAttributeName:[UIColorredColor]}range:NSMakeRange(0, [attributeStringlength])];
return attributeString;
}
- UIPickerView 的使用
- UIPickerView的使用
- UIPickerView 的使用
- UIPickerView的使用
- UIPickerView的使用
- UIPickerView的使用
- UIPickerView的使用
- UIPickerView的使用
- UIPickerView控件的使用
- UIPickerView的简单使用
- iOS UIPickerView的使用
- UIPickerView的简单使用
- UIPickerView的使用
- UIPickerView的使用
- UIPickerView的使用
- UIPickerView的使用
- UIpickerView的使用
- UIPickerView的使用
- 剑指Offer面试题3(Java版):二维数组中的查找
- nyoj 会场安排问题
- Android : ”找不到符号“错误
- 自己写一个快速开发android模板
- maven项目调试Junit类
- UIPickerView的使用
- cocos2dx中的回调函数CC_CALLBACK_*
- fiddler录制http请求
- Qt使用虚拟机windows共享目录调用第三方库编译不成功
- Android BaseAdapter和ViewHolder 优化 解决ListView的item抢焦点问题和item错乱问题
- 关于thinkphp模板引擎中变量输入的思考
- ZOJ 3427 Array Slicing (scanf使用)
- 常用函数/功能块 记录
- python 多线程