【问题标题】:Issue with dismissing UITextField in custom UITableViewCell在自定义 UITableViewCell 中关闭 UITextField 的问题
【发布时间】:2013-04-29 08:28:23
【问题描述】:

我在表格视图中实现了一个自定义单元格。 在单元格内,我有一个文本字段(设置为数字键盘)。 在表格视图中,我有 12 个单元格。 我可以用touchesBegan 删除键盘(尽管它仅在我点击我正在编辑的活动单元格时才有效)使用我放入自定义单元格类的代码。 如果我将键盘更改为字母和数字,我也可以关闭键盘。 我在键盘上有一个自定义的“完成”按钮,我希望它可以关闭文本字段。

我一直在尝试使用 endEditing:YES[cell.textFieldAnswer resignFirstResponder] 关闭键盘,但它不起作用。

有什么想法吗?

SecurityQuestions.m:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[GetQuestionsCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }


    NSInteger nextIndexPathRow = indexPath.row;
    nextIndexPathRow++;
    cell.labelQuestion.text = [arrayOfQuestions objectAtIndex:indexPath.row];
    cell.labelQuestionNumber.text = [NSString stringWithFormat:@".%d", nextIndexPathRow];

    switch (indexPath.row) {
        case 0:
            tfA1 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
            break;
        case 1:
            tfA2 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
            break;
        case 2:
            tfA3 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
            break;
        case 3:
            tfA4 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
            break;
        case 4:
            tfA5 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
            break;
        case 5:
            tfA6 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
            break;
        case 6:
            tfA7 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
            break;
        case 7:
            tfA8 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
            break;
        case 8:
            tfA9 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
            break;
        case 9:
            tfA10 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
            break;
        case 10:
            tfA11 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
            break;
        case 11:
            tfA12 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
            break;
    }
    cell.textFieldAnswer.tag = indexPath.row;
    cell.textFieldAnswer.delegate = self;
    [cell.textFieldAnswer setText:[answers objectAtIndex:indexPath.row]];
    return cell;
}

GetQuestionsCustomCell.m:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.textFieldAnswer resignFirstResponder];
}

编辑 1:

当我在做的时候:[cell performSelector@selector(KeyDown:)] 我有一个crach。 KeyDown 位于自定义单元格视图控制器内。 注意:'cell' 配置为我上面提到的自定义单元格。

错误:

线程 1:EXC_BAD_ACCESS(代码=2,地址=0x631ec)

编辑 2: 感谢“Thilina”在私人聊天中提供的所有帮助,现在它可以工作了,修复:

- (void)doneAction:(id)sender{ 
NSArray *cells = [self.tableView visibleCells]; 
for(UIView *view in cells){ 
if([view isMemberOfClass:[GetQuestionsCustomCell class]]){ 
GetQuestionsCustomCell *cell = (GetQuestionsCustomCell *) view; 
UITextField *tf = (UITextField *)[cell textFieldAnswer]; 
if([tf isFirstResponder]){ 
[tf resignFirstResponder]; 
} 
} 

} 
}

【问题讨论】:

  • customcell 的文本字段的属性名称是什么?
  • 请发布您的方法,- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  • 好的,已编辑并添加。

标签: ios uitableview keyboard uitextfield dismiss


【解决方案1】:

1) 将委托 UITextFieldDelegate 添加到您的 ViewController,

@interface ViewController ()<UITextFieldDelegate>

2) 实现 textFieldShouldReturn 方法

-(BOOL) textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

3) 在 tableView cellForRowAtIndexPath 方法中更改您的开关,

switch (indexPath.row) {
    case 0:{
        tfA1 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
        tfA1.delegate = self;
        }
        break;
    case 1:{
        tfA2 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
        tfA2.delegate = self;
    }break;
    case 2:{
        tfA3 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
        tfA3.delegate = self;
    }break;
    case 3:{
        tfA4 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
        tfA4.delegate = self;
    }break;
    case 4:{
        tfA5 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
        tfA5.delegate = self;
    }break;
    case 5:{
        tfA6 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
        tfA6.delegate = self;
    }break;
    case 6:{
        tfA7 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
        tfA7.delegate = self;
    }break;
    case 7:{
        tfA8 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
        tfA8.delegate = self;
    }break;
    case 8:{
        tfA9 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
        tfA9.delegate = self;
    }break;
    case 9:{
        tfA10 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
        tfA10.delegate = self;
    }break;
    case 10:{
        tfA11 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
        tfA11.delegate = self;
    }break;
    case 11:{
        tfA12 = (UITextField*)[cell viewWithTag:nextIndexPathRow];
        tfA12.delegate = self;
    }break;
}

【讨论】:

  • 添加和更改。当我单击一个按钮时,我将“resignFirstResponder”设置为所有 tf​​A1...12。我将日志设置为 textFieldShouldReturn ,但仍然没有。什么都没有发生。只有最重要的部分才开始工作。此外,如果重要,'textFieldDidEndEditing' 会调用。
  • 好的,您的 DONE 按钮的属性名称是什么?我可以通过自定义单元格访问按钮吗?
  • 太奇怪了,普通键盘会自行消失,但使用数字键盘不会。当我在父单元格中时,也许我可以执行位于自定义单元格中的选择器?
  • 是的,它在普通键盘上工作,但我不需要自定义“完成”按钮。
  • 您是否在 .h 中包含 KeyDown: 方法声明?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-21
  • 1970-01-01
  • 2011-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多