Search code examples
iphoneobjective-ciosuitextfield

How to make a UITextField in a table cell become first responder?


If I have a series of table cells, each with a text field, how would I make a specific text field become the first responder?


Solution

  • // Customize the appearance of table view cells.
        
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            UITextField *textField = [[UitextField alloc] init];
            // ...
            textField.delegate = self;
            textField.tag = indexPath.row;
            [cell addSubView:textField];
        }
    }
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        [textField resignFirstResponder];
        return YES;
    }