Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clicking on UITextField in a UITableViewCell

I have an issue where when a textField is clicked on in a UITableViewCell, the method tableView:didSelectRowAtIndexPath: does not get invoked. The problem is, I need to scroll my tableView into proper position, otherwise the keyboard goes right over the first responder.

I have to then move code like this:

[[self tableView] scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

into both my tableView delegate method and in my UITextField delegate method, textFieldDidBeginEditing:.

Is the best way to just create a new method, pass to it the indexPath of the cell/textfield being clicked, and call the method from both the tableView delegate and the UITextField delegate? better way of going about it?

like image 541
Coocoo4Cocoa Avatar asked Dec 08 '08 20:12

Coocoo4Cocoa


1 Answers

I found the following works well (It assumes you're in a table view controller)

- (void)textFieldDidBeginEditing:(UITextField *)textField{  
    CGPoint pnt = [self.tableView convertPoint:textField.bounds.origin fromView:textField];
    NSIndexPath* path = [self.tableView indexPathForRowAtPoint:pnt];
    [self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
like image 194
Friggles Avatar answered Oct 14 '22 01:10

Friggles