njyulan
njyulan

Reputation: 43

get indexPath for customCell in TableView

I have a UITextField in a customCell. I want to be able to get the text that user inputs in it. In my customCell.m file I can get the text that is inputed with

[self.aTextField addTarget:self action:@selector(aTextFieldEditing:) forControlEvents:UIControlEventEditingChanged];

but I cannot get the indexPath so I can save it to the NSMutableArray that holds all the UITextFields.

In my TableView I cannot get the didSelectRowAtIndexPath to run. I also cannot get the textFieldDidBeginEditing method to output anything. If it helps I have a TableView with sections.

My idea at the moment is to get the indexpath.row and indexpath.section from the tableView and save them as an extern so I can access it in the customCell.m

I would be grateful for any ideas or specific examples of how I could do this. Or a different cleaner way to accomplish what I want.

Upvotes: 0

Views: 287

Answers (1)

Lance
Lance

Reputation: 9012

You should be able to get the index path using the -indexPathForRowAtPoint: method on UITableView.

You can do something like this (not tested):

- (void)aTextFieldEditing:(UITextField *)textField {
    CGPoint convertedPoint = [textField.superview convertPoint:textField.center toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:convertedPoint];
    // Use your index path...
}

Edit:

In the case your cell is receiving the edit events, you can always define a protocol and make your view controller a delegate of the cell. You could then call this delegate method from your aTextFieldEditing method and pass the cell. Now that you have the cell you can call -indexPathForCell on your table view. If none of this makes sense, look into the delegate pattern. It's very common in Cocoa/Cocoa Touch and is very well documented online.

Upvotes: 1

Related Questions