【问题标题】:Skip to next UItextField跳到下一个 UItextField
【发布时间】:2013-12-12 08:53:37
【问题描述】:

我在 UItableView 中有一些 UITextfields。文本字段已在我创建的 UITableViewCell 中进行了子类化。在键盘上按下回车键后,我想知道如何向下移动到相应行中的下一个单元格。

因此,如果用户要从宽度开始,他们将继续沿着宽度列向下,或者如果他们想在每次按 Enter 时输入高度,他们将继续沿着该列向下。

目前发生的情况是,如果我使用宽度列,它可以正常工作,但如果我在按 Enter 后选择高度 UItextField,它会跳转到下一个宽度单元格。

这是我检测返回键的代码。

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [[self.view viewWithTag:textField.tag+1] becomeFirstResponder];
    return YES;
}

这就是我的单元格表格视图的样子。

有关更多信息,这是我将自定义单元格文本字段添加到 UItextfield 的方式。

NSString *widthString = [currentFinishingDictionary objectForKey:@"w"];
    if ((NSNull *) widthString != [NSNull null]) {
        cell.widthTexField.text = widthString;
        cell.widthTexField.delegate = self;
        cell.widthTexField.tag = indexPath.row;
    } else {
        cell.widthTexField.text = @" ";
        cell.widthTexField.delegate = self;
        cell.widthTexField.tag = indexPath.row;
    }

    NSString *heightString = [currentFinishingDictionary objectForKey:@"h"];
    if ((NSNull *) heightString != [NSNull null]) {
        cell.heightTextField.text = heightString;
        cell.heightTextField.delegate = self;
        cell.heightTextField.tag = indexPath.row;
    } else {
        cell.heightTextField.text = @" ";
        cell.heightTextField.delegate = self;
        cell.heightTextField.tag = indexPath.row;
    }

【问题讨论】:

  • 单个单元格是否包含 1 个宽度的文本字段和 1 个高度的文本字段?

标签: ios uitableview uitextfield


【解决方案1】:

我认为问题可能在于标签分配给 UITextFields 的方式。

请检查标签值是否真的是你假设的。

【讨论】:

  • 我正在为他们的标签分配 indexpath.row 编号,这会导致问题。我不知道还能怎么做。
  • 实际上.. 如果我在安装标签中添加 0.5,它应该可以工作,我 htink.. 不。
  • 我猜这意味着有两个 TextFields 具有相同的标签。如果是这种情况,您可以考虑给宽度文本字段编号 100,101.102 .. 等和高度字段 1000,1001,1002,1003 .. 等等,即在设置标签时添加额外的 100 或 1000。它可能会解决您的问题。
  • 是的。那可能我必须去或更大的数字,因为到目前为止我有多达 800 个文本字段......可能更多或更少......但这只是感觉有点丑陋的方式来做到这一点。 .
【解决方案2】:
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
      [yourtextfield resignFirstResponder];
      // save state, etc if necessary
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCell * cell = (MyCell *)[tableView cellForRowAtIndexPath:indexPath];
    self.selectedField = cell.tf; // cell.tf is a UITextField property defined on MyCell
    [yourtextfield becomeFirstResponder];
}

【讨论】:

    【解决方案3】:

    确保您已将标签设置为UITextField

    你应该这样尝试:

    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        //[[self.view viewWithTag:textField.tag+1] becomeFirstResponder];
        // Get here you cell seleted Refrecne
          id obj = [cell.contentView viewWithTag:textField.tag+1];
    
          if([obj isKindOfClass:[UITextField class]]) {
            UITextField *nextField = (UITextField*)obj
             [nextField becomeFirstResponder];
         } 
        return YES;
    }
    

    或者你可以使用ELCTextFieldCell

    它在一个单元格中包含 UITextField 和 UILabel。您可以根据需要进行定制。

    【讨论】:

    • obj 在这种情况下返回 nil。可能是因为我继承了 UItableViewCell?我也将使用 ELCTextFieldcell 但是由于其他视图以及表本身的复杂性,我致力于 UITavleview .. 它们不仅仅是这两个文本字段。当您对单元进行子类化时,性能明智的 UITabeViews 也很棒。
    • 如果obj 为零。那么你应该检查你是否给出了准确的标签值?确保当用户点击 UITextField 时您能够获得正确的单元格实例。如果 UITableView 中有很多东西,那么最好使用不同的单元格标识符。
    【解决方案4】:

    你可以使用

       - (BOOL)textFieldShouldReturn:(UITextField *)textField
        {
    
    
    UITableViewCell *cell = [self.myTableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow: textField.tag+1 inSection:0]];
    UITextField *tf = (UITextField*)[cell viewByTag:textField.tag+1]
    [tf becomeFirstResponder];
            return YES;
        }
    

    【讨论】:

      【解决方案5】:

      您是否尝试过使用collectionView 而不是tableview,可以使集合视图设计类似于您添加的图像,并且您可以在一个单元格中添加一个文本字段,然后通过这样做使宽度文本字段的第一响应者

      - (BOOL)textFieldShouldReturn:(UITextField *)textField
      {
      if ( [tableView.dataSource tableView:tableView numberOfRowsInSection:0] > tag + 2)
              CustomCell *cell = [self.myTableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow: textField.tag+2 inSection:0]];
      
              [cell.textField becomeFirstResponder];
      
      }
      

      如果你还想坚持这个设计,那么你可以这样做

      - (BOOL)textFieldShouldReturn:(UITextField *)textField
      {
      
           int xPosition = (int)textField.frame.origin.x;
      
           CustomCell *cell = [self.myTableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow: textField.tag+1 inSection:0]];
      
           if((int)cell.textField1.x == xPosition)
           {
                [cell.textField1.x becomeFirstResponder];
      
           }
          else
           {
                [cell.textField2.x becomeFirstResponder];
           }
      

      }

      代码未编译。只写了逻辑。希望它有所帮助:)

      【讨论】:

        【解决方案6】:

        如下更改您的分配标签值,这可能会对您有所帮助。

        NSString *widthString = [currentFinishingDictionary objectForKey:@"w"];
            if ((NSNull *) widthString != [NSNull null]) {
                cell.widthTexField.text = widthString;
                cell.widthTexField.delegate = self;
                cell.widthTexField.tag = indexPath.row;
            } else {
                cell.widthTexField.text = @" ";
                cell.widthTexField.delegate = self;
                cell.widthTexField.tag = indexPath.row;
            }
        
            NSString *heightString = [currentFinishingDictionary objectForKey:@"h"];
            if ((NSNull *) heightString != [NSNull null]) {
                cell.heightTextField.text = heightString;
                cell.heightTextField.delegate = self;
                cell.heightTextField.tag = indexPath.row + 2000;
            } else {
                cell.heightTextField.text = @" ";
                cell.heightTextField.delegate = self;
                cell.heightTextField.tag = indexPath.row + 2000;
            }
        

        现在您的 textfields 标签如下所示,可以按您的意愿工作。

        0 2000
        1 2001
        2 2002
        3 2003
        4 2004
        .. ........
        

        现在您的方法可以正常工作,但还需要考虑其他想法,因为当您有 2000 个细胞时,这会出错。

        - (BOOL)textFieldShouldReturn:(UITextField *)textField
        {
            // i.e. textField.tag == 6 then next 6 + 1 == 7
            // i.e. textField.tag == 20006 then next 2006 + 1 == 2007
            [[self.view viewWithTag:textField.tag+1] becomeFirstResponder];
            return YES;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-02-19
          • 1970-01-01
          • 2012-03-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-04-01
          相关资源
          最近更新 更多