UITextField inside UITableViewCell: becomeFirstResponder in didSelectRowAtIndexPath

妖精的绣舞 提交于 2019-12-04 06:28:25

If you have a custom cell, then you can do something like this:

@implementation CustomCell

#pragma mark - UIResponder

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (BOOL)becomeFirstResponder
{
    return [self.textField becomeFirstResponder];
}

@end

Then inside the table view delegate:

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

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.. :)

I believe you need to set you textField's delegate property.

Add textField.delegate = self; to your method, before you call [textField becomeFirstResponder]

I think you should include a tag value on the textfield when creating the cell:

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

And on the didSelectRowAtIndexPath method use the tag value to recover the object:

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

}

I've including a validation to control if the textfield is already the first responder.

Hope it works as you expect

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


}

I think it will be helpful to you.

Updated the answer from @josh-fuggle to use 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()
    }
}

And this is in your table view delegate:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        let cell = tableView.cellForRowAtIndexPath(indexPath)
        if ((cell?.canBecomeFirstResponder()) != nil) {
            cell?.becomeFirstResponder()
        }
}

You can do something like this:

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()
    }
}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!