Return NSIndexPath from custom cell ? (UITableView)
Just give the cell a value for its tag
property. Then you can get that cell by calling this
UITableViewCell *cell = (UITableViewCell *)[self.tableView viewWithTag:tagValue];
then once you have the cell you can get the NSIndexPath like this
NSIndexPath *indexPath = [self.tableView indexPathForCell:nextResponderCell];
Since you have a UITextField in your custom cell you can place cell.textField.delegate = self;
in the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
data source method. That way you will not have to setup a NSNotification in the Custom Cell. Also in this same method you can tag both your cells text field like this cell.textField.tag = indexPath.row;
and the cell like this cell.tag = indexPath.row;
Now that you have set the UITextField delegate, you can now place this method in your UITableViewController class
- (void)textFieldDidBeginEditing:(UITextField *)textField {
CustomCell *cell = (CustomCell *)[self.tableView viewWithTag:textField.tag];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
}
The above UITextField delegate method should get you the indexPath for the cell you have currently selected.
Related videos on Youtube
![JonasG](https://i.stack.imgur.com/LD8wI.jpg?s=256&g=1)
Comments
-
JonasG about 2 years
I need to get the
NSIndexPath
for a custom cell in aUITableView
. Here's the problem: I send aNSNotification
from my custom cell to myUITableViewController
wheneditingDidBegin
gets called from aUITextField
in my custom cell. In myUITableViewController
, I resize theUITableView
when theUITextField
began editing, and then want the table view to scroll to the Cell in which theUITextField
is first responder. But I can't figure out how to return theindexPath
of the cell where theUITextField
is being edited. I have tried so many ways, but its still not working. One way is this: in my cstomCell class i select the row using[self setSelected:YES]
and in my TV controller then if INSLog
the row of[self.tableV indexPathForSelectedRow]
it always returns 0 even though its always not 0. -
JonasG almost 13 yearsI don't get it, please provide some more code. There is no relation between the two lines of code, please tell me where to put which line of code.
-
ms83 almost 13 years@Maxner I have edited my answer. Hope that clears up any confusion.