在自定义UITableViewCell中解除UITextField的问题

时间:2013-05-05 11:54:07

标签: ios uitableview keyboard uitextfield dismiss

我在表视图中实现了一个自定义单元格。 在单元格内部,我有一个文本字段(设置为数字键盘键盘)。 在表格视图中我有12个单元格。 我可以使用touchesBegan来调用键盘(虽然只有当我点击我正在编辑的活动单元格时它才有效),并且我在自定义单元格类中放入了代码。 如果我将键盘更改为字母和数字,我也可以关闭键盘。 我在键盘上有一个自定义的“DONE”按钮,我想让它关闭文本字段。

我一直试图用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:)]我有一个笨蛋。 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]; 
} 
} 

} 
}

1 个答案:

答案 0 :(得分:5)

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