【问题标题】:change first responders in uitableviewcell textfield更改 uitableviewcell 文本字段中的第一响应者
【发布时间】:2015-04-01 14:39:26
【问题描述】:

我正在使用情节提要开发应用程序。 UITableViewCell (MoneyEntryTableViewCell) 有一个自定义类,其中包含 UITextField

问题: 当我在键盘上按上一个/下一个 (<>) 时,我想将焦点移到其他单元格中添加的其他文本字段。

在.h文件中

@interface MoneyEntryTableViewCell : UITableViewCell

@property (strong, nonatomic) IBOutlet UILabel *lblMemName;
@property (strong, nonatomic) IBOutlet UITextField *textAmount;
@property (strong, nonatomic) IBOutlet UILabel *lblmemId; // Hidden 

@end

在 .m 文件中

@implementation MoneyEntryTableViewCell

@synthesize lblMemName,textAmount;
- (void)awakeFromNib {
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end

在 Controller 中,这是我的 cellForRowAtIndexPath 函数...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    MoneyEntryTableViewCell *cell = (MoneyEntryTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"UserAmountCell"];
    if (!cell)
    {
         cell = [[MoneyEntryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UserAmountCell"] ;
    }
    cell.selectionStyle =UITableViewCellSelectionStyleNone;

    UILabel     *lblname  = (UILabel *)    [cell lblMemName];
    lblname.tag =100;
    lblname.text = [[self.MembersList objectAtIndex:indexPath.row] objectAtIndex:0];

    UILabel     *lblId  = (UILabel *)    [cell lblmemId];
    lblId.tag =101;
    lblId.text = [[self.MembersList objectAtIndex:indexPath.row] objectAtIndex:1];

    UITextField *txtfield = (UITextField *)[cell textAmount];
    txtfield.tag = 200 + indexPath.row;
    txtfield.placeholder = @"0.00";
    txtfield.delegate = self;
    [txtfield addTarget:self  action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    txtfield.inputAccessoryView = keyboardDoneButtonView;

    return cell;
}

【问题讨论】:

    标签: ios uitableview uitextfield first-responder


    【解决方案1】:

    您可以创建对要在其间跳转的文本字段的引用。设置单元格时,获取对文本字段的引用并进行设置。对于这个例子,我假设我已经声明了一个 NSMutableArray *textFieldArray

    // within cellForRowAtIndexPath
    self.textField1 = (UITextField*)[cell viewWithTag:100];
    [self.textField1.setDelegate:self];
    
    if (![textFieldArray containsObject:self.textField1]) {
        [textFieldArray addObject:self.textField1];
    }
    

    这样,当您识别出“下一步”按钮被按下时,跳转到 textFieldArray 中的下一个索引并使该文本字段成为第一响应者。为此,您可以跟踪哪个索引是第一响应者,或者遍历数组以找到处于活动状态的索引...

    for (int i = 0; i < [textFieldArray count]; i++) {
        if ([[textFieldArray objectAtIndex:i] isFirstResponder]) {
            if (i == [textFieldArray count]-1) // last item in array - dismiss
                [[textFieldArray objectAtIndex:i] resignFirstResponder];
            else // go to next item
                [[textFieldArray objectAtIndex:i+1] becomeFirstResponder];
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以通过将文本字段的引用保存在数组中来做到这一点

      - (void)switchToNextField:(BOOL)next
      {
          //pass next = YES if you want next text filed to become active
          //pass next = NO if you want previos text filed to become active
      
          for (id object in arrayOfTextFields) { //loop through all text fields
              if ([object isKindOfClass:[UITextField class]]) {
                  UITextField *textFieldObject = (UITextField *)object;
                  if ([textFieldObject isFirstResponder]) { //check if it is in editing state
                      float indexOfCurrentField = [arrayOfTextFields indexOfObject:textFieldObject];
                      if (next) {
                          indexOfCurrentField++;//if next button clicked
                      }
                      else
                      {
                          indexOfCurrentField-- ;//if previous button clicked
                      }
                      if (indexOfCurrentField < 0)
                          indexOfCurrentField = 0; //this will solve the previous button crash
                      if (indexOfCurrentField < arrayOfTextFields.count) {
                          UITextField *nextTextField = [arrayOfTextFields objectAtIndex:indexOfCurrentField];
                          [nextTextField becomeFirstResponder];
      
                      }
                  }
      
              }
          }
      }
      

      【讨论】:

      • 单击下一步按钮,为什么表格中的最后一个文本字段成为第一响应者。第一次单击上一个按钮它会因重复索引而崩溃,但我通过文本字段标记禁用上一个按钮来解决它。
      • 当你点击下一步按钮时,调用[self switchToNextField:YES];,当你点击上一个按钮时,调用[self switchToNextField:NO];
      • 谢谢赛义夫。我再次遇到同样的问题。移动到表中的最后一个字段。无论如何,我得到新的想法并解决我的问题。再次感谢。
      • 不客气,问题可能是arrayOfTextFields 中的文本字段引用不正确,请确保数组中文本字段的索引正确。
      【解决方案3】:

      @saif 谢谢。

      我在这里发布我的代码。

      在界面中声明。 UITextField *activeField;

      - (void)Previous:(id)sender
      {
          [self switchToNextField:NO];
      }
      
      - (void)Next:(id)sender
      {
          [self switchToNextField:YES];
      }
      

      按照您在UITextfield 中设置的标签执行您自己的逻辑来禁用和启用上一个和下一个,

      - (void)textFieldDidBeginEditing:(UITextField *)textField {
      
          activeField = textField;
          [self.PrevButton setEnabled:YES];
          [self.NextButton setEnabled:YES];
          if(textField.tag ==200)
              [self.PrevButton setEnabled:NO];
          else if (textField.tag == 200 + [self.MembersList count] - 1) //self.MembersList table Source.
          {
              [self.NextButton setEnabled:NO];
          }
      }
      
      - (void)textFieldDidEndEditing:(UITextField *)textField
      {
          activeField = nil;
      
      }
      

      如果您希望下一个文本字段变为活动状态,请传递 next = YES。 如果您希望前一个文本字段变为活动状态,请传递 next = NO

      - (void)switchToNextField:(BOOL)next
      {
          NSLog(@"%ld",(long)activeField.tag);
          if(next)
          {
              float tag = activeField.tag - 200;
              NSIndexPath *indexPathUserName = [NSIndexPath indexPathForRow:tag + 1 inSection:0];
              UITableViewCell *cell = (UITableViewCell *)[self.tblVwSpltList cellForRowAtIndexPath:indexPathUserName];
              UITextField *nextTextField = (UITextField *)[cell.contentView viewWithTag:activeField.tag + 1];
              [nextTextField becomeFirstResponder];
          }
          else
          {
              float tag = activeField.tag - 200;
              NSIndexPath *indexPathUserName = [NSIndexPath indexPathForRow:tag - 1 inSection:0];
              UITableViewCell *cell = [self.tblVwSpltList cellForRowAtIndexPath:indexPathUserName];
              UITextField *nextTextField = (UITextField *)[cell.contentView viewWithTag:activeField.tag - 1];
              [nextTextField becomeFirstResponder];
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-01-05
        • 2020-08-24
        • 2016-07-07
        • 2011-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多