Get row index of custom cell in UITableview

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.

  • Install .ipa to iPad with or without iTunes
  • iPhone : How to detect the end of slider drag?
  • Xcode Data Model Editor not appearing
  • Using UIPinchGestureRecognizer to scale uiviews in single direction
  • iPhone/iPad unique identifier BESIDES UUID/UDID?
  • UIGestureRecognizers vs touchesBegan/touchesMoved/touchesEnded (accuracy)
  • 5 Solutions Collect From Internet About “Get row index of custom cell in UITableview”

    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);
    

    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;
    

    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
    }
    

    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];
    

    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);