Get row index of custom cell in UITableview

24,829

Solution 1

Your TextView will be contained within some kind of cell. Once you have found this cell, you can ask the table for the index. Navigate from your TextView up the view hierarchy to find the cell. For example:

TextView* textView = // your textView;
UITableViewCell* cell = (UITableViewCell*)[textView superview];
UITableView* table = (UITableView *)[cell superview];
NSIndexPath* pathOfTheCell = [table indexPathForCell:cell];
NSInteger rowOfTheCell = [pathOfTheCell row];
NSLog(@"rowofthecell %d", rowOfTheCell);

Solution 2

its really Simple, in cellForRowAtIndexPath. just tag your textView like

cell.txtView.tag = indexPath.row;

in textView Delegate method find the row using following code (textViewDidBeginEditing)

int row = textView.tag;

Solution 3

In Swift running on iOS 8.4:

@IBAction func signInUpBigButtonPressed(sender: SignInUpMenuTableViewControllerBigButton) {
    // We set the self.indexCellContainingButtonClicked with the cell number selected.
    var parentCell: UIView! = sender
    do {
        parentCell = parentCell.superview!
    } while !(parentCell is UITableViewCell)
    let cell = parentCell as! UITableViewCell
    self.indexCellContainingButtonClicked = self.tableView.indexPathForCell(cell)!.row
}

Solution 4

This is a more elegant way to accomplish what you asked

CGPoint senderPosition = [sender convertPoint:CGPointZero toView:self.tableView];

NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:senderPosition];

Solution 5

Running IOS 8.3 found and implemented this within a textViewDidEndEditing
method; works well!

UIView *parentCell = sender
while (![parentCell isKindOfClass:[UITableViewCell class]]) {   // iOS 7 onwards the table cell hierachy has changed.
    parentCell = parentCell.superview;
}

UIView *parentView = parentCell.superview;

while (![parentView isKindOfClass:[UITableView class]]) {   // iOS 7 onwards the table cell hierachy has changed.
    parentView = parentView.superview;
}


UITableView *tableView = (UITableView *)parentView;
NSIndexPath *indexPath = [tableView indexPathForCell:(UITableViewCell *)parentCell];

NSLog(@"indexPath = %@", indexPath);
Share:
24,829
user1531912
Author by

user1531912

Updated on July 09, 2022

Comments

  • user1531912
    user1531912 almost 2 years

    I'm developing an ipad app. I have a UITableview in the app. The UITableview is created programatically with one label and textview as its subview. There can be a max of five rows in tableview. At a time 2 rows are displayed to the user. In runtime, its require to enter some text in the textview & save it in the local sqlite db. I have implemented the textViewDidBeginEditing and textViewDidEndEditing delegates in the code. In the textViewDidEndEditing delegate, Im trying to add/replace the text(entered in textview) in an NSMUtable array. For that the rowindex of the textview for which I enter the text is required. So that i can add/replace the respective row index in the array.

    Please let me know how to get the row index of the text view.

  • user1531912
    user1531912 over 11 years
    Thanks for your reply. This is working in some scenarios and in other scenarios it gives inappropriate row index. Like if i navigate from row 0 to row 5 then to row 4 then to row 0, in the textviewDidEndediting it shows the index path.row as 0 whereas it should show 3 which is the index of the row i edited.
  • tombeynon
    tombeynon almost 9 years
    Be careful if you're adding or removing cells from the tableView after setting this tag - the order will change, leaving tags with the wrong values.
  • toddg
    toddg almost 6 years
    Thank you! This is a much better solution than the rest in my opinion.