最近学习UIPickerView使用,练习时候想遇到些问题,总结一下。
用UIPickerView 显示7个组件,每个组件中的数据要居中对齐,1-6组件中字体颜色为红色,7组件中字体颜色为蓝色。
在控制器中创建两个实例变量:
UIPickerView *picker;
NSArray *datasourceArray; //用做数据源
因为UIPickerView中的文本不能设置颜色和对齐方式,所以delegate使用UILabel显示文本,并使用返回视图(UIView *)的方法:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
有三种方案可供选择:
1.不需要创建实例变量,直接在Delegate方法中创建UILabel,设置属性,并返回。
2.在初始化时,创建模板可变数组,并根据最大组件的总行数,把Label控件插入此可变数组中,再把此模板数组赋值给 dataSource。最后Delegate调用方法返回模板数组中的相应行的对象。
3.创建7个数组的实例变量,分别作为picker每个组件的数据源,再创建相应数量的UILabel,分别设置好。最后Delegate调用方法返回相应数据源中的对象。
02 |
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:( NSInteger )row forComponent:( NSInteger )component reusingView:(UIView *)view { |
04 |
UILabel *showLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f,0.0f,20.0f,20.0f)]; |
06 |
showLabel.text = [ NSString stringWithFormat:@ "%d" ,row+1]; |
07 |
showLabel.backgroundColor = [UIColor clearColor]; |
12 |
showLabel.textColor = [UIColor blueColor]; |
16 |
showLabel.textColor = [UIColor redColor]; |
20 |
showLable.textAlignment = UITextAlignmentCenter; |
22 |
return [showLabel autorelease]; |
2 在初始化时,创建模板可变数组,并根据最大组件的总行数,把Label控件插入此可变数组中,再把此模板数组赋值给 dataSource。最后Delegate调用方法返回模板数组中的相应行的对象。
04 |
NSMutableArray * mutableArray = [ NSMutableArray arrayWithCapacity:20]; |
06 |
for ( NSUInteger i = 1;i <= 33 ;i++) { |
08 |
UILabel *showLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f,0.0f,20.0f,20.0f)]; |
09 |
showLabel.text = [ NSString stringWithFormat:@ "%d" ,i]; |
10 |
showLabel.backgroundColor = [UIColor clearColor]; |
11 |
showLabel.textColor = [UIColor redColor]; |
12 |
showLable.textAlignment = UITextAlignmentCenter; |
13 |
[mutableArray addObject:showLabel]; |
18 |
self .datasourceArray = mutableArray; |
23 |
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:( NSInteger )row forComponent:( NSInteger )component reusingView:(UIView *)view { |
25 |
if (component == 6 ) { |
27 |
UILabel * myLabel = [ self .datasource objectAtIndex:row]; |
28 |
myLabel.textColor = [UIColor blueColor]; |
33 |
return [ self .datasource objectAtIndex:row]; |
但是到程序运行时出现问题,正常应该显示7个组件的内容,但现在只能显示一个组件的内容。并且只要旋转其它组件显示出来的内容与已经显示出来的相同,那么已经显示出来的内容就会消失。
例如:程序刚运行时第一个组件显示出了1,2,3.其它组件显示不出内容。如果旋转1组件,显示出4,5,6,7,8,再旋转2组件,当2组件显示出4,5,6,7,8时,1组件的就会消失。
也就是说,不同的组件,如果显示相同的内容时,只会显示最后旋转的组件。
应该是没有把mutableArray数组进行深度复制(Deep Copy)造成,修改原实例变量名为 datasource1,并增加6个新实例变量,datasource2,datasource3 ......datasource7,
再修改viewDidLoad如下:
03 |
NSMutableArray * mutableArray = [ NSMutableArray arrayWithCapacity:20]; |
05 |
for ( NSUInteger i = 1;i <= 33 ;i++) { |
07 |
UILabel *showLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f,0.0f,20.0f,20.0f)]; |
08 |
showLabel.text = [ NSString stringWithFormat:@ "%d" ,i]; |
09 |
showLabel.backgroundColor = [UIColor clearColor]; |
10 |
showLabel.textColor = [UIColor redColor]; |
11 |
showLable.textAlignment = UITextAlignmentCenter; |
12 |
[mutableArray addObject:showLabel]; |
17 |
for ( NSUInteger i = 1;i<=7 ;i++) { |
19 |
NSArray *newArray = [[ NSArray alloc] initWithArray:mutableArray copyItems: YES ]; |
20 |
[ self setValue:newArray forKey:[ NSString stringWithFormat:@ "datasource%d" ,i]]; |
修改delegate方法如下:
01 |
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:( NSInteger )row forComponent:( NSInteger )component reusingView:(UIView *)view { |
05 |
return [ self .datasource1 objectAtIndex:row]; |
07 |
} else if (component == 1){ |
09 |
return [ self .datasource2 objectAtIndex:row]; |
11 |
} else if (component == 2){ |
13 |
return [ self .datasource3 objectAtIndex:row]; |
15 |
} else if (component == 3){ |
17 |
return [ self .datasource4 objectAtIndex:row]; |