在 uitableviewcell 文本字段中更改第一响应者

change first responders in uitableviewcell textfield

我正在使用故事板开发应用程序。 UITableViewCell (MoneyEntryTableViewCell) 有一个自定义 class,其中包含 UITextField

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

在 .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;
}

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

// within cellForRowAtIndexPath
self.textField1 = (UITextField*)[cell viewWithTag:100];
[self.textField1.setDelegate:self];

if (![textFieldArray containsObject:self.textField1]) {
    [textFieldArray addObject:self.textField1];
}

这样,当您识别出 "next" 按钮被按下时,跳转到 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];
    }
}

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

- (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];

                }
            }

        }
    }
}

@saif 谢谢。

我post我的代码在这里。

在接口中声明。 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];
    }
}