Reputation: 16841
I need to add items to a UIDatePicker
, but instead of adding dates:
I need to add a list of names (e.g., "James"). How do I do that?
Upvotes: 0
Views: 279
Reputation: 2593
Check the following,
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, 250)];
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
pickerView.opaque = NO;
[customPickerView addSubview:pickerView];
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return 10;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
ILabel* tView = (UILabel*)view;
tView.text = [arrList objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//When ever picker view cell is pressed.
//Write ur logic
}
Upvotes: 2
Reputation: 15628
Just use UIPickerView
UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
myPickerView.delegate = self
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *string = [NSString stringWithString:@"Jack"];
return string;
}
EDIT:1
- (NSInteger)selectedRowInComponent:(NSInteger)component
{
//Returns the index of the selected row in a given component.
NSLog(@"%d",component);
}
Upvotes: 3