Reputation: 943
I have several UIPickerViews in my viewController.
And this is one of them need to be customized since it displays 2 UILabels in 1 picker row.
And I use these delegate methods:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
// this method is use for normal pickers, and I would judge whether the picker that calling
// this method is a normal one or the special one by pickerView.
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view {
// and this one is specially for the special picker, I would also judge the pickerView is
//normal or special, if pickerView is normal, return nil, else I return a UIView with
//2 UIlabels.
}
But now after my debugging, I found that if I implement the 2 methods together, the second one is always called and the first one seems never to be called,
and it results in that my special picker displays correct data, but others have nothing.
How could I do this?
if I give all the pickers' data in the second method, would the reusingView become a problem, since the special picker's reusingView is not in the same format as other picker?
Thanks a lot!
Upvotes: 3
Views: 1283
Reputation: 15
I would further add to ABOVE response by 'govi'; 'tag' property (for this you have to assign different 'tag' values to each UIPicker view, i.e. 1,2,3...) can also be used to identify multiple UIPikeViews; e.g.,
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
if (pickerView.tag ==0) {
}
else if (pickerView.tag ==1) {
UILabel *lbl = [[UILabel alloc] init];
lbl.text = [self pickerView:pickerView titleForRow:row forComponent:component];
//do some more styling like setting up the font as bold
//adding some padding to the text and some shiny things
return lbl;
}
}
Further more you can use any of these
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if (pickerView.tag ==0) { }
else if (pickerView.tag ==1) { } }
This is one especially used when u want you Picker to simply display some value. whereas the above one is used if you want to add some Customization to the displayed Label (i.e Width, heigth, Font...)..
Upvotes: 1
Reputation: 686
Using a different delegate isnt that straightforward though. Create a separate class,
@interface MyCustomDelegate : NSObject <UIPickerViewDelegate>
and implement the delegate methods, in this case
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
// this method is use for normal pickers, and I would judge whether the picker that calling
// this method is a normal one or the special one by pickerView.
}
Once you do this, create an instance of this class and set it as the delegate like..
pickerView.delegate = [[MyCustomDelegate alloc] initWithData:data] autorelease];
where the data might be the data you might want to include to use in the delegate methods for the titles.
You can use the same delegate instance for all the pickers, or can create separate instances. The depends a lot on the kind of data you have. If they are all unrelated to each other, it would be nice to use separate instances, else you could go ahead with just the one.
Well that's that.
As for the second one, you would be doing something like this
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
if (pickerView==specialPickerView) {
}
else {
UILabel *lbl = [[UILabel alloc] init];
lbl.text = [self pickerView:pickerView titleForRow:row forComponent:component];
//do some more styling like setting up the font as bold
//adding some padding to the text and some shiny things
return lbl;
}
}
Ofcourse, you would be doing this in your existing class.
Upvotes: 1