UITableViewCell中的UITextField:didSelectRowAtIndexPath中的becomeFirstResponder

我目前正试图让这个工作。 任何帮助表示赞赏。

我有一个自定义的UITableViewCell里面有一个UITextField 。 当我select表格单元格时,我想使UITextField第一个响应者。
然而[textField becomeFirstResponder]; 返回false

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; UITextField *textField; for (UIView *view in [cell subviews]) { if ([view isMemberOfClass:[UITextField class]]) { textField = ((UITextField *)view); } } [textField becomeFirstResponder]; } 

根据要求,文本字段的初始化。 这是在UITableViewCell子类中完成的:

 - (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Init Textfield _textField = [[UITextField alloc] init]; _textField.backgroundColor = [UIColor clearColor]; _textField.delegate = self; [self addSubview:_textField]; } return self; } 

如果你有一个自定义的单元格,那么你可以做这样的事情:

 @implementation CustomCell #pragma mark - UIResponder - (BOOL)canBecomeFirstResponder { return YES; } - (BOOL)becomeFirstResponder { return [self.textField becomeFirstResponder]; } @end 

然后在表格视图中委托:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; if ([cell canBecomeFirstResponder]) { [cell becomeFirstResponder]; } } 

为每个UITextField tag并使用以下代码:

 UITableViewCell* cell = (UITableViewCell*)sender.superview.superview; UITextField *txtField = (UITextField*)[cell viewWithTag:tag]; [txtField becomeFirstResponder]; 

didSelectRowAtIndexPath方法中。

它的工作非常好.. 🙂

我相信你需要设置你的textFielddelegate属性。

添加textField.delegate = self; 到你的方法,在你调用[textField becomeFirstResponder]

我想你应该在创build单元格时在文本字段中包含一个标签值:

 - (id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Init Textfield _textField = [[UITextField alloc] init]; _textField.backgroundColor = [UIColor clearColor]; _textField.delegate = self; _textField.tag = 999; [self addSubview:_textField]; } return self; } 

然后在didSelectRowAtIndexPath方法中使用标签值来恢复对象:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; UITextField *textField = (UITextField*)[cell viewWithTag:999]; if (![textField isFirstResponder]){ [textField becomeFirstResponder]; } } 

我已经包含一个validation来控制文本字段是否已经是第一个响应者。

希望它能像你期望的那样工作

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; UITextField *textField; for (id subV in [cell subviews]) { if ([subV isKindOfClass:[UITextField class]]) { textField = (UITextField *)subV; [textField becomeFirstResponder]; break; } } } 

我认为这对你有帮助。

更新@ josh-fuggle的答案来使用Swift 2.2。

 import Foundation import UIKit class CustomTableCell:UITableViewCell { @IBOutlet var textValue: UITextField! override func canBecomeFirstResponder() -> Bool { return true } override func becomeFirstResponder() -> Bool { return self.textValue.becomeFirstResponder() } } 

这是在你的表视图委托:

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let cell = tableView.cellForRowAtIndexPath(indexPath) if ((cell?.canBecomeFirstResponder()) != nil) { cell?.becomeFirstResponder() } }