Pergunta

I have created a custom cell that places a UITextField in each row of a UITableView. What I would like to do is enable the UITextField for a particular row (i.e. activate the cursor in that textfield), when the user presses on anywhere inside the cell.

I have the following method where I try to do this:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

     [_cell.textField setUserInteractionEnabled:TRUE];

}

However, this is not working. Can anyone see what I'm doing wrong?

Foi útil?

Solução

You're on the right track placing this inside didSelectRowAtIndexPath:. However, setting user interaction enabled on the textfield just means that the user would be allowed to interact with the object if they tried. You're looking for becomeFirstResponder.

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    [cell.textField becomeFirstResponder];
}

Outras dicas

Try this code:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

     [_cell.textField becomeFirstResponder];
}

What is this _cell iVar? Are you setting it somewhere?

I think what you are trying to accomplish is this:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
     [cell.textField becomeFirstResponder];
}

Notice that in this way you are getting the cell for the tapped indexPath.

And also, just for information, it's a pattern in objective-C to use booleans as YES or NO, instead of true or false.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top