【问题标题】:How to shift the control on the next UITextField when return key is press按下返回键时如何在下一个 UITextField 上移动控件
【发布时间】:2015-10-14 12:34:09
【问题描述】:

我有一个带有自定义UITableViewCellUITableViewController。每个单元格包含一个UITextField。现在我想要的是在按下返回键时将控件从一个文本字段转移到下一个文本字段。 我要做的是这个……

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSIndexPath *currentIndexPath=[self.tableView indexPathForCell:(UITableViewCell*)textField.superview.superview];
    if(currentIndexPath.row<_excerciseData.totalBlanks-1){
        NSIndexPath nextIndexPath=???
        UITextField *nextTextFeild=[(UITableViewCell*)[self.tableView cellForRowAtIndexPath:nextIndexPath] viewWithTag:2];
        [nextTextFeild becomeFirstResponder];
    }else{

[textField resignFirstResponder];
}
return YES;
}

但我不知道如何找到下一个UITableViewCell 的 indexPath。谁能帮我找到这个?这... :)

【问题讨论】:

  • 绝对不要使用.superview.superview
  • @Wain 为什么不用这个...你能解释一下吗?
  • @Wain 是正确的。因为 textField.superview.superview 将无法获取某些 iOS 版本的 tableviewcell。
  • 视图层次结构可能会随着操作系统的版本而改变。避免对视图进行类型转换,因为您不确定以后会发生什么。此外,您可能会发现自己处于将字段添加到新子视图的位置,并且需要更改此代码。如果你必须做你所做的至少尝试这样: UITableViewCell *cell = myTextField; while ([cell isKindOfClass:[UITableViewCell class]] == NO) { cell = cell.superview; if(cell == nil) { 休息; } }

标签: ios iphone uitableview uitextfield


【解决方案1】:

由于您已经有一个自定义表格视图单元格,最简单的解决方案是添加一个保存索引路径的属性。然后在返回单元格的委托方法中,只需将当前索引路径分配给以后可能使用的单元格。

这对于部分中的最后一个单元格或表格视图本身来说效果不佳。您应该做的是将事件从单元格本身报告给表视图所有者(视图控制器),然后可以计算下一个单元格索引是什么:如果您有多个部分,它必须检查它是否应该转到一个新部分并且如果它是最后一个单元格,则什么也不做(如果您愿意,也可以转到表格视图的开头)。

对于转发消息,您可以尝试此处答案中提到的程序之一:IOS: Navigate through tableView and show table view data on next view

为了清楚起见,文本字段委托应该是单元格本身!

【讨论】:

  • 根据您的解决方案,如果我将当前索引路径分配给 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { } 委托中的 indexPath 属性,然后,在最后它将包含最后一个可见单元格的索引路径... 1. m 对吗?
  • 如果是,我不需要这个索引路径....我实际上需要所选单元格的索引路径
  • 不,单元格本身包含索引路径,这意味着您可能同时拥有多个。由于文本字段委托将向单元格本身报告此单元格将具有正确的索引路径...
  • 必须调用 cell.indexPath = indexPath。而不是 cellForRow 方法中的 self.indexPath = indexPath。
【解决方案2】:

你最好这样做,

CGPoint pointInTable = [field convertPoint:field.bounds.origin toView:_tableView];    

NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:pointInTable];

参考:How to get a UITableViewCell from one of its subviews

或者这样做,

id view = [textfield superview];

while (view && [view isKindOfClass:[UITableViewCell class]] == NO) {
    view = [view superview]; 
}

UITableViewCell *tableViewCell = (UITableViewCell *)view;

参考:How to get UITableView from UITableViewCell?

【讨论】:

    【解决方案3】:

    您可以更好地使用 textField 的 tag 属性。这样做。

    当您使用cellForRowAtIndexPath 创建单元格时 为单元格内的 textField 提供标记值,例如:

    cell.yourTextField.tag = indexPath.row + someConstant; // someConstant is just to make sure that there is no textfield with the same tag value
    

    并在您的 textField 委托中:- (BOOL)textFieldShouldReturn:(UITextField *)textField 实现以下内容。

    // Jumping with the keyboard Next button implementation.
    if (UIReturnKeyNext == textField.returnKeyType) //may be u can avoid this check
    {
        id nextField = [self viewWithTag:textField.tag + 1];
        if ([nextField isKindOfClass:[UITextField class]])
        {
            [nextField becomeFirstResponder];
        }
    }
    

    希望 textFieldDelegates 在 tableview 的所有者类中实现。

    【讨论】:

    • 其实还不错......也许只是这样做:tag = indexPath.section * 10000 + indexPath.row + someConstant
    • 是的,这样会减少碰撞的机会。
    【解决方案4】:

    解决办法

    #prgma mark - UITableViewDelegate Methods
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
       static NSString *stringCell = @"Cell";
       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
       if (cell == nil)
       {
         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
       }
      cell.yourTextField.tag = indexPath.row;
      return cell;
    }
    
    #prgma mark - UITextFieldDelegate Methods
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
         NSUInteger index = textField.tag;
         UITextField *nextTextField = (UITextField*)[self.view viewWithTag:index+1];
         [nextTextField becomeFirstResponder];
         return YES;
    }
    

    【讨论】:

    • 为什么要在这里检查:if (index == 6) ?
    猜你喜欢
    • 1970-01-01
    • 2012-01-02
    • 2011-12-28
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多