カスタム UITableView セルがあります
@interface PickTypeCell : UITableViewCell <UIPickerViewDataSource, UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *txtTextField;
@property (readwrite, retain) IBOutlet UIPickerView* dtpPicker;
@end
実装ファイルは次のようになります
@implementation PickTypeCell
@synthesize txtTextField;
@synthesize dtpPicker;
- (void)awakeFromNib
{
NSLog(@"inside PickTypeCell init");
//assign this to the picker delegate / datasource
dtpPicker = [[UIPickerView alloc] init];
dtpPicker.dataSource = self;
dtpPicker.delegate = self;
// assign the picker to the text field input view,so the picker is displayed when the text field receives input event
txtTextField.inputView=dtpPicker;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
//retuns the number of columns of the picker view : 1
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
//return the number of rows for each component : the number of tree types
return 5;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
//called to get the text to be displayed on each row of the picker
return @"HAHAH";
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//called when the user clicks on a row of the picker view
txtTextField.text=@"HABOOOOUN";
}
セルが読み込まれると、入力テキスト フィールドが表示されます。テキスト フィールドをクリックすると、予想どおり、ピッカー ビューに「HAHAH」を含む 5 行が表示されます。行をクリックすると、テキスト フィールドのテキストが「HABOOOOUN」文字列で更新されます。
しかし、ピッカーはまだ表示されており、それを閉じる方法がわかりません。
ユーザーがピッカーの任意の行をクリックすると、ピッカーが閉じられるようにしたいと思います。ユーザーがテキスト フィールドを再度クリックすると、ピッカーが再び表示されます。
「didSelectRow」メソッドで [dtpPicker rejectFirstResponder] を使用してみましたが、成功しませんでした。なんで?