2

I've seen examples of picker views used where the values for the picker are hardcoded into the source code like this

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSString * title = nil;
 switch(row) {
            case 0:
                title = @"a";
                break;
            case 1:
                title = @"b";
                break;
            case 2:
                title = @"c";
                break;
        }

However, if you're going to have 100 numbers in your picker view that would be very impractical. I'm sure you can see what I'm trying to do below. It's giving me the error

expression is not an integer constant expression

How can I get the numbers from 0 to 100 in a picker view? Feel free to comment if there's a better way to get input where a user selects a number between 1 and 100.

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSString * title = nil;

    if (pickerView.tag == 1) // this is otherPickerview
    {
        otherpickerview
        for (int i = 2; i < 100; i++){
            switch(row) {
                case i:
                    title = [NSString stringWithFormat:@"%d", i];
                    break;

            }


        }

    }
Leahcim
  • 40,649
  • 59
  • 195
  • 334
  • What was wrong with your commented array based solution? That's how I'd recommend you do this. – Mick MacCallum Apr 21 '14 at 03:19
  • 1
    I have no idea why you would use a switch statement to do this. You do it just like a table view by accessing an array element based on the row and component values. I'm not sure what examples you've seen, but they're bad ones. – rdelmar Apr 21 '14 at 03:21
  • @0x7fffffff that was actually someone else's code. Didn't realize how it would apply to my situation. – Leahcim Apr 21 '14 at 03:21

2 Answers2

1

Let's try:

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _arr.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
// you can switch to other UIPickerView, and return title for row which you want.
// don't need to loop whenever titleForRow is called.
return _arr[row];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.navigationController setNavigationBarHidden:YES];

_arr = [[NSMutableArray alloc]init];
for (int i = 0; i<100; i++) {
    NSString *str = [NSString stringWithFormat:@"%d",i];
    [_arr addObject:str];
}
_pickerView.dataSource = self;
_pickerView.delegate = self;
}
nmh
  • 2,497
  • 1
  • 15
  • 27
0

This can be done in a much simpler fashion using an array. All you have to do is create your array with all the possible titles in it, and then access them based on the current row in the titleForRow: method. Here's an example:

- (NSArray *)items
{
    static NSArray *items = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        items = @[@"a",@"b",@"c"]; // ..... etc ...
    });

    return items;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

    if (pickerView.tag == 1) {
        NSArray *items = [self items];

        if (row < items.count) {
            return [self items][row];
        }

        return nil;

    } else {
        return @"something else";
    }
}

Additionally, if your data is strictly alphanumerical characters it is possible to create an array from the characters in a NSCharacterSet, see this:

NSArray from NSCharacterset

Community
  • 1
  • 1
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
  • @Leahcim Because that method will be accessed once for every row in the picker view, and there's no sense in recreating the array every time if it isn't necessary. – Mick MacCallum Apr 21 '14 at 03:31