I have created a custom view that contains a UIPickerView
and a UITableViewCell
to display the contents currently selected in the picker. My PickerViewController
is set as both the delegate
and dataSource
of the picker, and I have implemented numberOfComponentsInPickerView:
, pickerView: numberOfRowsInComponent:
, and pickerView: titleForRow: forComponent:
, but the picker appears blank when the program is run, and thanks to placing NSLog
calls in the methods, I've discovered that the methods are never getting called. I have no idea what the issue is, as the picker appears to be linked up correctly with the PickerViewController
.
For the basic functionality that I'm trying to achieve, look at the "Start & End" view in the iOS Calendar app. I am trying to imitate this exactly, but only using one UITableViewCell
instead of three.
PickerViewController.h
#import <UIKit/UIKit.h>
@interface PickerViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITableViewCell *pickerSelection;
// Property to dynamically determine what content to display in the picker.
@property (nonatomic) BOOL userIsChoosingClass;
@end
viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(self.thePicker.delegate.description);
if (self.userIsChoosingClass) {
self.pickerSelection.textLabel.text = @"Class";
} else {
self.pickerSelection.textLabel.text = @"Major";
}
}
Picker View Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
size_t numberOfRows = 0;
if (self.userIsChoosingClass) {
numberOfRows = [self.brain classChoicesForSignUp].count;
} else {
numberOfRows = [self.brain majorChoicesForSignUp].count;
}
return numberOfRows;
}
Picker View Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
NSLog(@"Picker view delegate method called");
NSString *title;
if (self.userIsChoosingClass) {
title = [[self.brain classChoicesForSignUp] objectAtIndex:row];
} else {
title = [self.majorChoices objectAtIndex:row];
}
return title;
}
If your numberOfRows... datasource method is returning 0, then your delegate method will never be called - the picker won't ask for the title of a row if it doesn't think it has any rows to display.