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.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With