2

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

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

有任何想法吗?

安全问题.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]; 
} 
} 

} 
}
4

1 回答 1

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;
}
于 2013-05-05T12:10:00.407 回答