Get custom cell row index in UITableview

I am developing an ipad application. I have a UITableview in an application. A UITableview is created programmatically with a single label and text as a subquery. A tableview can have no more than five rows. At that time, 2 lines are displayed to the user. At run time, you need to enter some text in the text box and save it in the local db SQL block. I implemented the textViewDidBeginEditing and textViewDidEndEditing delegates in the code. In the delegate textViewDidEndEditing, I am trying to add / replace text (entered in text form) in an NSMUtable array. This requires the string index of the text field for which I entered the text. So that I can add / replace the corresponding row index in the array.

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

+6
source share
5 answers

Your TextView will be contained in some cell. When you find this cell, you can query the table for the index. Scroll up the view hierarchy from the TextView to find the cell. For instance:

 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); 
+20
source

its really simple, in cellForRowAtIndexPath . just mark your textView as

 cell.txtView.tag = indexPath.row; 

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

 int row = textView.tag; 
+4
source

Swift runs 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 } 
+3
source

This is a more elegant way to accomplish what you requested.

 CGPoint senderPosition = [sender convertPoint:CGPointZero toView:self.tableView]; NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:senderPosition]; 
+2
source

The launch of iOS 8.3 is found and implemented in the text editor 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); 
+1
source

All Articles