UI控件之UIPickerView的协议方法
UIPickerView:选择视图,父类是UIView
UIPickerView *pickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(10, 100, 300, 50)];
设置代理,通过代理设置显示样式和内容
pickerView.dataSource=self;
pickerView.delegate=self;
当列之间有关联时,要重新设置某列的值的同时进行刷新列,可以全部刷新,也可以刷新某一列
[pickerView reloadAllComponents];
[pickerView reloadComponent:2];
当pickerView重新成为第一响应者的时候刷新数据
[pickerView reloadInputViews];
pickerView的协议方法
<UIPickerViewDataSource,UIPickerViewDelegate>
设置列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
设置每列的行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component==0){
return 1;
}else{
return 2;
}
return _dataArray.count;
}
设置每列每行显示的view
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
//第1列显示内容
if(component==0){
}else{ //第2列显示内容
}
}
设置每列中项的高度
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
if(component==0){
return 60;
}else{
return 40;
}
}
设置每列的宽度
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
if(component==0)
return 200;
else
return 100;
}
当用户选择某行内容时执行,row是用户选择的行,component是用户选择的行所在的列
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//如果用户操作的是第1列
if(component==0){
NSString *img=_dataArray[row];
//获取界面上当前显示的行数(在第几列)
NSInteger index=[pickerView selectedRowInComponent:1];
NSString *name=_dataArray[index];
NSLog(@"you choose image,%@,%@",img,name);
}else{
NSString *name=_dataArray[row];
NSInteger index=[pickerView selectedRowInComponent:0];
NSString *img=_dataArray[index];
NSLog(@"you choose name,%@,%@",img,name);
}
}
设置每列每行上显示的文本内容,component是列,row是行
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component==0){
return @"Good";
}else{
if(row==0){
return @"Morning";
}else{
return @"Night";
}
}
}