我已经创建了一个自定义视图,该视图包含一个UIPickerView和一个UITableViewCell,以显示当前在选择器中选择的内容。我的PickerViewController被设置为选择器的delegate和dataSource,我已经实现了numberOfComponentsInPickerView:、pickerView: numberOfRowsInComponent:和pickerView: titleForRow: forComponent:,但是当程序运行时,选择器看起来是空的,由于在方法中放置了NSLog调用,我发现这些方法永远不会被调用。我不知道问题是什么,因为选择器似乎与PickerViewController连接正确。
关于我正在尝试实现的基本功能,请查看iOS日历应用程序中的"Start & End“视图。我正试图准确地模仿这一点,但只使用一个UITableViewCell而不是三个。
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;
@endviewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(self.thePicker.delegate.description);
if (self.userIsChoosingClass) {
self.pickerSelection.textLabel.text = @"Class";
} else {
self.pickerSelection.textLabel.text = @"Major";
}
}选择器视图数据源方法
- (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;
}选择器视图代理方法
- (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;
}发布于 2012-06-11 17:21:12
如果你的numberOfRows..。datasource方法返回0,那么您的委托方法将永远不会被调用--如果选择器不认为它有任何行可显示,它就不会请求行的标题。
https://stackoverflow.com/questions/10955323
复制相似问题