Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 737
user1531912 Avatar asked Dec 05 '12 12:12

user1531912


4 Answers

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);
like image 80
ColinE Avatar answered Nov 13 '22 02:11

ColinE


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;
like image 21
junaidsidhu Avatar answered Nov 13 '22 00:11

junaidsidhu


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
}
like image 3
King-Wizard Avatar answered Nov 13 '22 01:11

King-Wizard


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];
like image 3
Franco Carbonaro Avatar answered Nov 13 '22 01:11

Franco Carbonaro