Question

I've a custom cell I implement in table view. Inside the cell I've a text field (set to number pad keyboard). Inside the table view I've 12 cells. I can dusmiss the keyboard with touchesBegan (although it is work only when I tap the active cell I'm editing) with code I put in the custom cell class. I can also dismiss the keyboard if I change the keyboard to letters and numbers. I've a custom 'DONE' button on the keyboard which I want it to dismiss the text field.

I've been trying for a while to dismiss the keyboard with endEditing:YES or [cell.textFieldAnswer resignFirstResponder] and it doesn't work.

Any ideas?

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

EDIT 1:

When I'm doing: [cell performSelector@selector(KeyDown:)] I have a crach. The KeyDown is located inside the custom cell view controller. Note: 'cell' is configured as the custom cell as I mention above.

Error:

Thread 1: EXC_BAD_ACCESS (code=2, address=0x631ec)

EDIT 2: Thanks to 'Thilina' for all of his help in private chat, now it works, the fix:

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

} 
}
Was it helpful?

Solution

1) Add the delegate UITextFieldDelegate to your ViewController,

@interface ViewController ()<UITextFieldDelegate>

2) implement the textFieldShouldReturn method

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

3) change your switch in the tableView cellForRowAtIndexPath method,

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top