6

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

我有一个UITableViewCellUITextField内在的习惯。当我选择表格单元格时,我想做第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;
}
4

8 回答 8

16

如果您有自定义单元格,则可以执行以下操作:

@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];
    }
}
于 2014-08-13T01:50:05.373 回答
3

Give tag for each UITextField and and use Following code:

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

In didSelectRowAtIndexPath method.

Its working very good.. :)

于 2013-04-04T10:49:02.993 回答
2

我相信你需要设置你textFielddelegate财产。

在调用之前添加textField.delegate = self;到您的方法中[textField becomeFirstResponder]

于 2013-04-04T11:09:26.933 回答
2

我认为您应该在创建单元格时在文本字段中包含一个标记值:

- (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];
  }

}

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

希望它按您的预期工作

于 2013-05-02T14:41:12.430 回答
0
- (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;
    }
  }


}

我想这会对你有所帮助。

于 2013-04-04T11:01:33.700 回答
0

更新了@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()
        }
}
于 2016-06-10T16:03:59.523 回答
0

你可以这样做:

class CustomCell: UITableViewCell {
    @IBOutlet weak var textField: UITextField!

    override func awakeFromNib() {
        super.awakeFromNib()
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(contentViewTapped))
        self.contentView.addGestureRecognizer(tapGesture)
    }
    @objc func contentViewTapped() {
        _ = self.textField.becomeFirstResponder()
    }
}
于 2018-07-24T06:29:24.997 回答
-2

[self.titleLab performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.2];

于 2018-05-09T03:51:53.743 回答