首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITextField in UITableViewCell: becomeFirstResponder in didSelectRowAtIndexPath

UITextField in UITableViewCell: becomeFirstResponder in didSelectRowAtIndexPath
EN

Stack Overflow用户
提问于 2013-04-04 10:43:41
回答 8查看 16.8K关注 0票数 6

我现在正努力让这件事发挥作用。任何帮助都是非常感谢的。

我有一个定制的UITableViewCell,里面有一个UITextField。当我选择表格单元格时,我想要使UITextField第一响应程序。

但是,[textField becomeFirstResponder];返回false

代码语言:javascript
复制
- (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];
}

根据请求,初始化textfield。这是在UITableViewCell子类中完成的:

代码语言:javascript
复制
- (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;
}
EN

回答 8

Stack Overflow用户

发布于 2014-08-13 01:50:05

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

代码语言:javascript
复制
@implementation CustomCell

#pragma mark - UIResponder

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

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

@end

然后在表视图委托中:

代码语言:javascript
复制
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if ([cell canBecomeFirstResponder]) {
        [cell becomeFirstResponder];
    }
}
票数 16
EN

Stack Overflow用户

发布于 2013-04-04 10:49:02

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

代码语言:javascript
复制
UITableViewCell* cell = (UITableViewCell*)sender.superview.superview;
UITextField *txtField = (UITextField*)[cell viewWithTag:tag];
[txtField becomeFirstResponder];

didSelectRowAtIndexPath方法中。

它工作得很好..。:)

票数 3
EN

Stack Overflow用户

发布于 2013-04-04 11:09:26

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15808936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档