ios iphone 自定义UIPickerView
来源:互联网 发布:cad for mac打不开 编辑:程序博客网 时间:2024/04/30 22:36
在自己定义UIPickerView的时候,遇到的各种问题,小小总结一下。
1、获取当前的时间:
NSDate *date = [NSDate date];
NSDateFormatter *monthFormatter = [[[NSDateFormatter alloc] init] autorelease];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init] autorelease];
[monthFormatter setDateFormat:@"M"];
[dateFormatter setDateFormat:@"d"];
NSString *formatterMonthString = [monthFormatter stringFromDate:date];————这里输出的是月份的数字
NSString *formatterDateString =[dateFormatter stringFromDate:date]; ————日期数字
2、UIPickerView的UIPickerViewDataSource和UIPickerViewDelegate方法。
注意:
**********this view controller is the data source and delegate*********self.datePickerView.delegate = self;
self.datePickerView.dataSource = self;
****************UIPickerViewDataSource**************
@required(必须实现的方法)
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;————返回一个数字,表示UIPickerView显示有多少列
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;————返回数字,表示每一列显示的行数。
注意:若每一列显示的行数不一样,可以用swich语句分别进行定义。(以两列为例)
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSInteger integer;
if (component == 0){
integer=3;————第一列有3行
}
else{
switch ([pickerView selectedRowInComponent:0])————第一列的每一行对应第二列里的行数
{
case 0:
integer=3;
break;
case 1:
integer=4;
break;
case 2;
integer=4;
break;
}
}
return integer;
}
**************** UIPickerViewDelegate**************
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component————这个方法是定义在PickerView接收点击,滚动,选择事件的时候所做出的反映。
{
if (component == 0) {
if (row >0) {
[pickerView reloadAllComponents];————点击第一列中的每一行,第二列中的行数都reload一遍。
}
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component————这个方法是定义PickerView的各列中每一行显示的文字
{
NSString *returnStr = @"";
if (pickerView == self.datePickerView)
{
if (component == 0)————第一列显示的文字
{
returnStr = [monthArray objectAtIndex:row];————monthArray是第一列所显示内容的数组
}
else{
switch ([pickerView selectedRowInComponent:0]) {
case 0:
{
returnStr=[[self.dateDic objectForKey:[self.monthArray objectAtIndex:0]] objectAtIndex:row];————要看自己的工程才明白字典和数组的内容了= = ||
break;
}
case 1:
{
returnStr=[[self.dateDic objectForKey:[self.monthArray objectAtIndex:1]] objectAtIndex:row];
break;
}
case 2:
{
returnStr=[[self.dateDic objectForKey:[self.monthArray objectAtIndex:2]] objectAtIndex:row];
break;
}
}
}
}
return returnStr;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component————这个方法定义每一列的宽度
{
CGFloat componentWidth = 0.0;
if (component == 0)
{
componentWidth = 100.0;
}
else
{
componentWidth = 90.0;
}
return componentWidth;
}
//PickerView出现时默认显示当天的月份和日期
[self.datePickerView selectRow:[self.monthLable.text intValue]-1inComponent:0 animated:YES];
[self.datePickerView selectRow:[self.dayLable.text intValue]-1inComponent:1 animated:YES];
原文:http://hi.baidu.com/%B6%AF%D0%C4c%B5%F7/blog/item/454e9cd09284c6cda8ec9a32.html
- ios iphone 自定义UIPickerView
- [IOS]UIPickerView(自定义选择器)
- UIPickerView 自定义
- 自定义UIPickerView
- iOS UIPickerView
- iOS UIPickerView
- iOS UIPickerView
- IOS UIPickerView
- IOS UIPickerView
- iphone开发 UIAlertView+ UIPickerView
- iOS学习之自定义弹出UIPickerView或UIDatePicker(动画效果)
- iOS学习之自定义弹出UIPickerView或UIDatePicker(动画效果)
- ios 自定义弹出UIPickerView或UIDatePicker(动画效果)
- iOS 自定义UIPickerView天数选择器视图 —— HERO博客
- iOS 自定义UIPickerView地区选择器视图 —— HERO博客
- 【UIPickerView自定义选择器控件】
- 自定义时间选择器UIPickerView
- iOS-iphone自定义状态栏
- Android成长之路-Android组件_CheckBox例子小解
- K&R C Exercise 3-1 Solution
- Android成长之路-Android组件_ListView例子小解
- Latex 自定义颜色
- Android成长之路-Android组件_Radio 例子小解
- ios iphone 自定义UIPickerView
- Android成长之路-Android组件_Spinner例子小解
- ActiveX异步回调JavaScript
- VC2005从开发MFC ActiveX ocx控件到发布到.net网站的全部过程
- MFC下的文档和视图以及框架之间的访问
- 如何用Javascript捕获ActiveX对象的事件
- ActiveX异步回调JavaScript(通过事件方式) .
- IT相关网站列表
- linux内核中链表的实现