giuseppe giuseppe - 2 months ago 15
iOS Question

Get the row of UITextField inside UITableViewCell

I have two

UITextFields
inside a custom cell of a
UITableView
.
I need to edit and store values of the textFields.
When I click inside a
UITextField
I have to know the row it belongs to in order to save the value to the correct record of a local array.
How can I get the row index of the textField?
I tried :

-(void)textFieldDidBeginEditing:(UITextField *)textField
{

currentRow = [self.tableView indexPathForSelectedRow].row;


}


But the currentRow does not change when I click inside the UITextFieldRow.It changes only when I click (select) the entire row...

Answer

Try this

//For ios 7

UITableViewCell *cell =(UITableViewCell *) textField.superview.superview.superview;
NSIndexPath *indexPath = [tblView indexPathForCell:cell];


//For ios 6

UITableViewCell *cell =(UITableViewCell *) textField.superview.superview;
NSIndexPath *indexPath = [tblView indexPathForCell:cell];