【问题标题】:To move to next text fieds in table cell移动到表格单元格中的下一个文本字段
【发布时间】:2011-05-21 11:13:48
【问题描述】:

在我的应用程序中,有一个表格视图,每个单元格有 2 个文本字段。我为每个文本字段分配标签。在按下完成按钮时,我想让下一个文本字段成为第一响应者(可以在表格单元格之间)。但是我现在使用的代码不起作用。附上当前代码

    NSArray *viewsToBecomeFirstResponder = [tableView subviews];
for (UIView *firstResponder in viewsToBecomeFirstResponder) {
    if ([firstResponder isKindOfClass:[UITextField class]]) {
        if (firstResponder.tag == nextTag ) {
            [firstResponder becomeFirstResponder];

        }

    }

}

我没有得到子视图数组中的文本字段。我正在为表格单元格使用自定义单元格。请帮忙。

【问题讨论】:

    标签: iphone uitableview uitextfield becomefirstresponder


    【解决方案1】:

    只需对 Karim 的代码稍作修改,您就可以像这样访问 textVies:

    - (void)textViewDidEndEditing:(UITextView *)textView {
       //Get the indexPath for the currently selected cell
       NSIndexPath *cellPath = [(UITableView*)[self view] indexPathForSelectedRow];
    
       //Get the actual cell
       CustomTableCell *cell = (CustomTableCell *)[(UITableView*)[self view] cellForRowAtIndexPath:cellPath];
    
       int currentTag = textView.tag;
       int nextTag = currentTag + 1; // or something else
    
       UITextView *nextTV = [cell viewWithTag: nextTag];
       [nextTV becomesFirstResponder];
    }
    

    希望这会有所帮助。

    【讨论】:

    • @xydev,你能说得更具体点吗?什么不工作?可能有很多错误,不一定是上面的代码。
    【解决方案2】:

    定义如下所列的方法——

    - (UITextField *) nextToRespondAfterTextField:(UITextField*)textField {
        NSInteger nextTag = textField.tag + 1;
    
        CustomTableCell *theCell = (CustomTableCell*)[textField.superview];
        for ( UIView *aView in theCell.subviews ) {
            if ( [aView isKindOfClass:[UITextField class]]) {
                if ( aView.tag == nextTag ) {
                    return aView;
                }
            }
        }
    
        NSIndexPath *indexPath = [self.tableView indexPathForCell:theCell];
    
        NSInteger section          = indexPath.section;
        NSInteger row              = indexPath.row;
        NSInteger numberOfSections = [self.tableView numberOfSections];
    
        if ( [self.tableView numberOfRowsInSection:section] != (row + 1) ) {
            indexPath = [NSIndexPath indexPathForRow:(row + 1) inSection:section];
        } else {
            do {
                section++;
            } while ( section < numberOfSections && [self.tableView numberOfRowsInSection:section] == 0 ) ;
    
            if ( section == numberOfSections ) {
                return nil;
            } else {
                indexPath = [NSIndexPath indexPathForRow:0 inSection:section];
            }
        }
    
        theCell = (CustomTableCell*)[self.tableView cellForRowAtIndexPath:indexPath];
        if ( theCell == nil ) {
            return nil;
        }
    
        for ( UIView *aView in theCell.subviews ) {
            if ( [aView isKindOfClass:[UITextField class]]) {
                if ( aView.tag == nextTag ) {
                    return aView;
                }
            }
        }   
    
        return nil;
    }
    

    在其他地方,

    UITextField *nextField = [self nextToRespondAfterTextField:textField];
    [nextField becomeFirstResponder];
    

    【讨论】:

      【解决方案3】:

      因为您使用标签来识别您的文本视图。另一种方法是使用文本视图代理。

      - (void)textViewDidEndEditing:(UITextView *)textView {
      int currentTag = textView.tag;
      int nextTag = currentTag + 1; // or something else
      UITextView *nextTV = [self.tableView viewWithTag: nextTag];
      [nextTV becomesFirstResponder];
      }
      

      【讨论】:

      • 没用..文本字段在表格单元格而不是表格视图中..我们不会得到间接子视图..
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-28
      相关资源
      最近更新 更多