Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Populate a static UIPickerView

I have a profile form as part of a registration process for an iOS app.

I would like to use a 'drop down' menu for items such as gender, title, DOB etc. The data for each will be static - I will use the UIPickerView to implement - but my question is - do I need to create arrays and data delegates to populate each individual picker or is there a simpler way to apply static data?

like image 852
Dancer Avatar asked Jan 17 '14 17:01

Dancer


1 Answers

Can you do it without a delegate? No.

Can you do it without an array? Yes, but you shouldn't.

Here is an example without an array (from http://cocoamatic.blogspot.com/2010/08/create-uipickerview-programmatically.html).

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
    // Handle the selection
}

// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    NSUInteger numRows = 3;

    return numRows;
}

// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
 return 1;
}

// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSString *title;

    title = [@"" stringByAppendingFormat:@"Row %d",row];

    return title;
}

// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
 int sectionWidth = 300;

 return sectionWidth;
}

However, you really should just use an array it makes it so much easier to read an maintain. If you want to add an additional value, you just add it to your array. Instead of updating in multiple spots, just add another value to your array. Also, much easier to understand.

@implementation PickerViewController
.
.
- (void)viewDidLoad {
      [super viewDidLoad];
      _myArray = @[@"Row 1", @"Row 2", @"Row 3",];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
    // Handle the selection
}

// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    NSUInteger numRows = 3;

    return _myArray.count;
}

// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
 return 1;
}

// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSString *title;

    title = myArray[row];

    return title;
}

// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
 int sectionWidth = 300;

 return sectionWidth;
}

If you had multiple pickers, you would just use multiple arrays and check each PickerView to see which one you had since the PickerView is passed into each of the functions listed.

@implementation PickerViewController
.
.
- (void)viewDidLoad {
      [super viewDidLoad];
      _myArray1 = @[@"Row 1", @"Row 2", @"Row 3",];
      _myArray2 = @[@"Row 1-2", @"Row 2-2", @"Row 3-2", @"Row 4-2"];      

      UIPickerView pickerView1 = [[UIPickerView alloc] init];
      UIPickerView pickerView2 = [[UIPickerView alloc] init];      
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
    // Handle the selection
    if (pickerView == pickerView1)
    {
        // First Picker
    }
    else if (pickerView == pickerView2)
    {
        // First Picker    
    }
}

// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (pickerView == pickerView1)
    {
        // First Picker
         return _myArray1.count;
    }
    else if (pickerView == pickerView2)
    {
        // Second Picker    
        return _myArray2.count;
    }    

    // A third picker passed in somehow
    return 0;
}

// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    if (pickerView == pickerView1)
    {
        // First Picker
         return 1;
    }
    else if (pickerView == pickerView2)
    {
        // Second Picker    
        return 1;
    }    

    // A third picker passed in somehow
    return 0;
}

// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSString *title;

    if (pickerView == pickerView1)
    {
        // First Picker
         title = myArray1[row];
    }
    else if (pickerView == pickerView2)
    {
        // Second Picker    
        rtitle = myArray2[row];
    }    

    return title;
}

// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (pickerView == pickerView1)
    {
        // First Picker
         return 300;
    }
    else if (pickerView == pickerView2)
    {
        // Second Picker    
        return 400;
    }    

    // A third picker passed in somehow
    return 0;
}
like image 154
ansible Avatar answered Sep 30 '22 12:09

ansible