【问题标题】:iOS) HIding Keyboard in a cell in UITableViewiOS)在 UITableView 的单元格中隐藏键盘
【发布时间】:2013-01-23 19:34:41
【问题描述】:
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"searchCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    searchField = (UITextField *)[cell viewWithTag:10000];

    [searchField resignFirstResponder];
}

好的,我有一个标识符名称为“searchCell”的单元格。此单元格包含一个标签为 10000 的 UITextField,它会在屏幕底部弹出一个键盘。当用户触摸另一个单元格时,必须隐藏键盘,以便用户有更大的空间上下滚动。

但是,当键盘弹出并且用户触摸(选择)一个单元格时,上面的代码被调用但不起作用...... :( 看起来分配的 UITableViewCell 不是用户当前使用的那个. 我在这里做错了什么?

【问题讨论】:

  • 添加 'forIndexPath' 根本没有帮助。
  • 您的 nib 和 .h 文件中是否设置了代表?也这样做 => UITextField *txtFieldObj = (UITextField *)[cell viewWithTag:10000];也在 didselectRowAtIndexpath 中执行此操作,添加一个 if(yourIndexpath.row==0) 然后只辞职键盘或 if(yourindexpath!=0) 然后辞职,否则做其他事情.. [txtFieldObj resignFirstResponder];还有 NSLog(@"txtField %@",textFieldObj); // 检查是否为非空..
  • 您是否设置了文本字段的委托?如果没有尝试textfield searchfield.delegate = self; 并在-(BOOL)textFieldShouldReturn:(UITextField *)textField 方法中调用[searchField resignFirstResponder];
  • 非常感谢你们:D
  • 哦,我也应该使用 didSelectRowAtIndexPath.. 而不是 'didDeselect...' :)

标签: ios uitableview uitextfield first-responder


【解决方案1】:
  1. 让你的班级成为 UITextField 的代表
  2. 转到 Storyboard 文件,单击文本字段并转到连接检查器
  3. 在 outlets 下,将代理连接到 View Controller
  4. 在模拟器中运行它。它会起作用的

【讨论】:

  • 完全不知道我必须将代理连接到视图控制器。
猜你喜欢
  • 1970-01-01
  • 2014-12-13
  • 2014-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多