最近学习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调用方法返回相应数据源中的对象。
//实现第1方案:- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { UILabel *showLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f,0.0f,20.0f,20.0f)]; showLabel.text = [NSString stringWithFormat:@"%d",row+1]; showLabel.backgroundColor = [UIColor clearColor]; //如果是第7个组件,字体颜色设置为蓝色。其余组件设置为红色。 if (component == 6) { showLabel.textColor = [UIColor blueColor]; }else { showLabel.textColor = [UIColor redColor]; } showLable.textAlignment = UITextAlignmentCenter; return [showLabel autorelease];}//这样每次旋转组件时都会创建一个新对象,并放入自动释放池中,如果以后大量旋转,会创建巨量对象。
2 在初始化时,创建模板可变数组,并根据最大组件的总行数,把Label控件插入此可变数组中,再把此模板数组赋值给 dataSource。最后Delegate调用方法返回模板数组中的相应行的对象。
//实现第2方案:-(void) viewDidLoad { NSMutableArray * mutableArray = [NSMutableArray arrayWithCapacity:20]; 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 redColor]; //需要显示红色组件多,默认使用红色. showLable.textAlignment = UITextAlignmentCenter; [mutableArray addObject:showLabel]; [showLabel release]; }//模板初始化完成 self.datasourceArray = mutableArray;}- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { if (component == 6 ) { //如果是第7个组件,返回对象字体颜色设置为蓝色。其余组件设置为红色。 UILabel * myLabel = [self.datasource objectAtIndex:row]; myLabel.textColor = [UIColor blueColor]; return myLabel; }else { 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如下:
-(void) viewDidLoad { NSMutableArray * mutableArray = [NSMutableArray arrayWithCapacity:20]; 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 redColor]; //需要显示红色组件多,默认使用红色. showLable.textAlignment = UITextAlignmentCenter; [mutableArray addObject:showLabel]; [showLabel release]; }//模板初始化完成 //为每个实例变量deep copy 一份模板数据. for(NSUInteger i = 1;i<=7 ;i++) { NSArray *newArray = [[NSArray alloc] initWithArray:mutableArray copyItems:YES]; [self setValue:newArray forKey:[NSString stringWithFormat:@"datasource%d",i]]; //想当于 self.datasource(1-7) = newArray [newArray release]; }}
修改delegate方法如下:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { if(component == 0 ) { 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]; }else { UILabel * myLabel = [self.datasource7 objectAtIndex:row]; myLabel.textColor = [UIColor blueColor]; return myLabel; }}
运行程序,这次运行直接退出,打开控制台找到错误提示,原来是因为模板数组中的对象是UILabel,而UILabel没有遵循NSCopying协议,不能进行深度复制。看来第2方案行不通了。
3 创建 UILabel存到Datasource的数组中。delegate直接返回数组中的UILabel。优点是调用委托方法时直接返回相应数据对像即可,缺点是需要先把全部的UILabel创建完成。
//实现第3方案:-(void) viewDidLoad { NSMutableArray * mutableArray = [NSMutableArray arrayWithCapacity:20]; NSUInteger j=0; for (NSUInteger i = 1; i <= 7 ;i++) { j++; 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]; if(j == 7) { showLabel.textColor = [UIColor blueColor]; }else { showLabel.textColor = [UIColor redColor]; } showLable.textAlignment = UITextAlignmentCenter; [mutableArray addObject:showLabel]; [showLabel release]; } [self setValue:mutableArray forKey:[NSString stringWithFormat:@"datasource%d",i]; }}- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { if(component == 0 ) { 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]; }else { return [self.datasource7 objectAtIndex:row]; }}
这次程序运行与第1方案相同,每次旋转组件时,不会创建任何对象。但是在程序初始化时会一次性创建全部所需对像。
总结:
第1方案 动态创建对象,适用于所需对象较多,但旋转较少的情况。(每个组件,每次显示一行,都会调用Delegate方法)。
第3方案 静态创建对象,适用于旋转较多的情况。这样每次调用Delegate方法不会生成任何对象。
多个组件需要使用不同的对象(UILabel).
UILabel不能深度复制.