Search code examples
iphoneiosobjective-cnsmutablearrayuipickerview

How to make my UIPickerView display all elements of an array


I have a UIPickerView and an array though I can't seem to be able to input all the date from the array into the UIPicker.

I know that the syntax is meant to be like this:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
   return [treatments objectAtIndex:row];
}

Where treatments is the array name though when I use this it comes up with this error:

-[Treatment isEqualToString:]: unrecognized

I have searched my entire project and can't find the phrase: isEqualToString

Treatment.h file:

#import <Foundation/Foundation.h>

@interface Treatment : NSObject {
    NSString *treatmentid;
    NSString *treatmentName;
    NSString *treatmentPrice;
}

@property (nonatomic, strong) NSString *treatmentid;
@property (nonatomic, strong) NSString *treatmentName;
@property (nonatomic, strong) NSString *treatmentPrice;

@end

All picker codes:

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return [treatments count];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [treatments objectAtIndex:row];
}

If you need any more code just say

Thanks in advance


Solution

  • you your Array contain Dictionar or NSMutableDictionar then you must specify value of array with it's key like bellow try to writtern you method like this:

    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
       return[[treatments objectAtIndex:row]valueForKey:@"YourKey"];
    }
    

    Here is basic Implementation of Picker view sample example code:-

    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thepickerView
    {
        return 1;
    }
    
    - (NSInteger)pickerView:(UIPickerView *)thepickerView numberOfRowsInComponent:(NSInteger)component
    {
            return [treatments count];
    }
    

    customize displaying lable of UIpickerview like bellow:-

    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
    {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
    
    
    
            if (component == 0) {
    
                label.font=[UIFont boldSystemFontOfSize:22];
                label.textAlignment = UITextAlignmentCenter;
                label.backgroundColor = [UIColor clearColor];
    
    
    
                label.text = [NSString stringWithFormat:@"%d", row];
                label.font=[UIFont boldSystemFontOfSize:22];
    
                 NSLog(@"%@",[yourpickerview selectedRowInComponent:component]);
    
            }
    
        return label;
    
    }
    - (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
    
       NSLog(@"%@",[treatments objectAtIndex:row]);
    
    }