0

我有一个自定义 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";
}

加载单元格时,将显示输入文本字段。单击文本字段时,选择器视图显示为 5 行包含“HAHAH”,如预期的那样。单击一行时,文本字段中的文本将更新为“HABOOOOUN”字符串。

但是选择器仍然显示,我不知道如何关闭它。

当用户单击选择器中的任何行时,我希望选择器被关闭。如果用户再次单击文本字段,则应再次显示选取器。

我尝试在“didSelectRow”方法中使用 [dtpPicker resignFirstResponder],但没有成功。为什么?

4

2 回答 2

4

你应该尝试辞职UItextField [txtTextField resignFirstResponder]

于 2013-10-18T18:36:16.263 回答
0
PickTypeCell *cell = (id)[tableView cellForRowAtIndexPath:indexPath];
[cell.txtTextField resignFirstResponder];

如果您点击 textField 或发送消息[textField becomeFirstResponder]textField 成为第一响应者,键盘会自动出现。如果您想隐藏键盘,您应该调用[textField resignFirstResponder];您可以将键盘更改为其他视图textField.inputView = myView;,您已经完成了

于 2013-10-18T18:44:42.767 回答