自定义UITableViewCell中的多个UITextField

时间:2012-09-14 16:33:21

标签: objective-c uitableview custom-cell becomefirstresponder

我有一个UITableView,它有两个不同的自定义单元定义。一个是单个UITextField,另一个是4个UITextFields

手动设置userInteractionEnabled以启用单元级触摸导航,并将didSelectRowAtIndexPath中的UI交互处理到相关单元的第一响应者

当我只使用一个带有一个UITextField(editableTextField)的customcell(EditableCustomCell)时,这一切都运行正常,但现在我有一个带有4个UITextFields(度,分,秒,笛卡儿)的customcell(LatLonCustomCell),我无法确定触摸了哪个字段以便设置yesFirstResponder

(目前我在调试期间默认为第一个名为degrees的文本域)

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{      
  [prevField resignFirstResponder];
  prevField.userInteractionEnabled = NO;

  if(indexPath.section == kFirstSection && (indexPath.row == kLatitudeRow || indexPath.row == kLongitudeRow)) {
    LatLonCustomCell *customCell = (LatLonCustomCell *)[MyTableView cellForRowAtIndexPath:indexPath];
    currField = customCell.degrees; // need to set correct field here
  } else {
    EditableCustomCell *customCell = (EditableCustomCell *)[MyTableView cellForRowAtIndexPath:indexPath];
    currField = customCell.editableTextField;
  }

  currFieldIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
  currField.userInteractionEnabled = YES;
  [currField becomeFirstResponder];

}

3 个答案:

答案 0 :(得分:1)

好的,对于那些遇到相同或类似问题的人来说,我终于取得了突破

我决定在调用didSelectRowAtIndexPath之前需要捕获触摸的X / Y坐标。这样我就可以通过检查文本字段“边界”的触摸来确定触摸发生在哪个UITextField

经过一些随机搜索,我发现在视图控制器中捕获任何触摸事件非常简单(因为touchesBegan只发生在自定义重写的UITableViewCell类中,我不知道如何将其传递回链单元> TableView >滚动视图>控制器)

将此添加到viewDidLoad方法:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
tapGesture.numberOfTapsRequired = 1;
// Pass the tap through to the UITableView
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];

这会捕获所有触摸,调用handleTapGesture方法

然后在这个方法中,只是检查触摸是否在tableview的范围内,如果是,确定触摸点的indexPath,然后检查所需对象的边界,下面是一个我想出的简化版

-(void)handleTapGesture:(UITapGestureRecognizer *)tapGesture {

  CGPoint tapLoc = [tapGesture locationInView:self.tableView];

  if([MyTableView indexPathForRowAtPoint:tapLoc]) {
    // Tap still handled by the UITableView delegate method

    NSIndexPath *indexPath = [MyTableView indexPathForRowAtPoint:tapLoc];

    if(indexPath.section == 0 && (indexPath.row == kLatitudeRow || indexPath.row == kLongitudeRow)) {
      LatLonCustomCell *customCell = (LatLonCustomCell *)[MyTableView cellForRowAtIndexPath:indexPath];
      UIScrollView *scrollView = (UIScrollView *)self.view;
      CGRect rc;

      // Degrees
      rc = [customCell.degrees convertRect:[customCell.degrees bounds] toView:scrollView];

      if (tapLoc.x >= rc.origin.x && tapLoc.y >= rc.origin.y && tapLoc.x <= (rc.origin.x + rc.size.width) && tapLoc.y <= (rc.origin.y + rc.size.height)) {
        NSLog(@"touch within bounds for DEGREES");
        touchField = customCell.degrees;
      }

// Repeat for other textfields here ....

....

在我的代码中,我将字段保存在touchField中,就像在didSelectRowAtIndexPath代码中一样,我已经处理了prevField / currField值来控制userInteractionEnabled的启用/禁用并将currField设置为becomeFirstReponder

希望这证明对某人有帮助:))

答案 1 :(得分:0)

过去当我需要检查文本框是否被触摸时,我检查了YourTextField.text.length&gt; 0.如果是,您可以设置becomeFirstResponder。希望这会有所帮助。

答案 2 :(得分:0)

您是否考虑过使用NSNotificationCenter为UITextFieldTextDidBeginEditingNotification请求通知?

在viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldBeganEditing) 
                                             name:UITextFieldTextDidBeginEditingNotification object:nil];

然后像

-(void) textFieldBegainEditing: (NSNotification*) notification {
  // [notification object] will be the UITextField
  // do what you need to do with it (resign, become first responder)
}