user1107173
user1107173

Reputation: 10754

UITableView getting the indexPath in textFieldDidBeginEditing:

I have a dynamic tableView. A couple of the cells have a textField.

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(15,10,260,40)];
textField.delegate = self;
[cell addSubview:textField];

I'm trying to get the indexPath in textFieldDidBeginEditing. Here is my code

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGPoint location = [self.view.superview convertPoint:self.view.center toView:self.orderTableView];
    NSIndexPath *indexPath = [self.orderTableView indexPathForRowAtPoint:location];
    NSLog (@"IndexPath %@", indexPath);
    NSLog (@"IndexPath.ROW %i", indexPath.row);  
}

I have the first 4 cells (index 0 - 3) that are standard cells. After the first 4 cells I can dynamically have upto 3 custom cells with UITextField.

After the UIViewController appears with the UITableView, and NO matter what TableViewCell with UITextField I click on, the NSLog Below keeps showing me the following:

2013-07-09 07:21:40.910 MyApp[2884:c07] IndexPath <NSIndexPath 0xa0a5d60> 2 indexes [0, 3]
2013-07-09 07:21:40.910 Mypp[2884:c07] IndexPath.ROW 3

How can I get the correct indexPath of the cell with UITextField that called the Keyboard?

Upvotes: 1

Views: 1338

Answers (1)

Chris Tetreault
Chris Tetreault

Reputation: 1973

I think you're on the right track. But I believe the point you're referencing, (the center of the view controller's view), won't give you an accurate location. As long as your textfield's frame stays within the cell, I believe this should work.

//convert the textfield's frame origin in the cell to frame origin in table view
CGPoint location = [self.tableView convertPoint:textField.frame.origin fromView:textField.superview];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];

Upvotes: 4

Related Questions