最近学习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]; |
19 |
} else if (component
== 4){ |
21 |
return [ self .datasource5
objectAtIndex:row]; |
23 |
} else if (component
== 5){ |
25 |
return [ self .datasource6
objectAtIndex:row]; |
29 |
UILabel
* myLabel = [ self .datasource7
objectAtIndex:row]; |
30 |
myLabel.textColor
= [UIColor blueColor]; |
运行程序,这次运行直接退出,打开控制台找到错误提示,原来是因为模板数组中的对象是UILabel,而UILabel没有遵循NSCopying协议,不能进行深度复制。看来第2方案行不通了。
3 创建 UILabel存到Datasource的数组中。delegate直接返回数组中的UILabel。优点是调用委托方法时直接返回相应数据对像即可,缺点是需要先把全部的UILabel创建完成。
NSMutableArray *
mutableArray = [ NSMutableArray arrayWithCapacity:20]; |
for ( NSUInteger i
= 1; i <= 7 ;i++) { |
for ( NSUInteger i
= 1;i <= 33 ;i++) { |
UILabel
*showLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f,0.0f,20.0f,20.0f)]; |
showLabel.text
= [ NSString stringWithFormat:@ "%d" ,i]; |
showLabel.backgroundColor
= [UIColor clearColor]; |
showLabel.textColor
= [UIColor blueColor]; |
showLabel.textColor
= [UIColor redColor]; |
showLable.textAlignment
= UITextAlignmentCenter; |
[mutableArray
addObject:showLabel]; |
[ self setValue:mutableArray
forKey:[ NSString stringWithFormat:@ "datasource%d" ,i]; |
-
(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:( NSInteger )row
forComponent:( NSInteger )component
reusingView:(UIView *)view { |
return [ self .datasource1
objectAtIndex:row]; |
} else if (component
== 1){ |
return [ self .datasource2
objectAtIndex:row]; |
} else if (component
== 2){ |
return [ self .datasource3
objectAtIndex:row]; |
} else if (component
== 3){ |
return [ self .datasource4
objectAtIndex:row]; |
} else if (component
== 4){ |
return [ self .datasource5
objectAtIndex:row]; |
} else if (component
== 5){ |
return [ self .datasource6
objectAtIndex:row]; |
return [ self .datasource7
objectAtIndex:row]; |
这次程序运行与第1方案相同,每次旋转组件时,不会创建任何对象。但是在程序初始化时会一次性创建全部所需对像。
总结:
第1方案 动态创建对象,适用于所需对象较多,但旋转较少的情况。(每个组件,每次显示一行,都会调用Delegate方法)。
第3方案 静态创建对象,适用于旋转较多的情况。这样每次调用Delegate方法不会生成任何对象。
多个组件需要使用不同的对象(UILabel).
UILabel不能深度复制.