UITextField inside UITableViewCell: getFirstResponder in didSelectRowAtIndexPath file

I'm currently trying to get this to work. Any help is appreciated.

I have a custom UITableViewCellone that has UITextFieldinside. When I select a table cell, I would like to make the UITextFieldfirst responder.
However, [textField becomeFirstResponder];returns 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];
}

As requested, initialization of the text field. This is done in a subclass 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;
}
+5
source share
6 answers

If you have a custom cell, 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];
    }
}
+11
source

tag UITextField :

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

didSelectRowAtIndexPath .

..:)

+3

, textField delegate.

textField.delegate = self; , [textField becomeFirstResponder]

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

}

, .

, , .

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


}

, .

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

And this is in the table view delegate:

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

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

All Articles