Reputation: 1564
I have a table view with dynamic cells constructed with autolayout
. I have a UITextView
as subview inside UITableViewCell . This UITextView
can grow vertically i.e scroll is disabled for this text view and it's height constraint is equal to cell content view's height. So whenever textview height increases then corresponding cell's height will also increase.
I need to get indexpath of any given cell.
When textview height becomes higher than screen height then indexPathForCell
returns nil. I also tried indexPathForRowAtPoint:[cell center]
it also returns nil. I cannot get indexpath from my model cells array since model don't have my cell (cell was already deleted from model array).
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableArray *cellArray = [tableViewCellsDictionary objectForKey:[NSNumber numberWithUnsignedLong:sectionIndex]];
return [cellArray objectAtIndex:rowIndex];
}
-(void) updateTextViewTextInCell:(CustomCell *) cell {
NSIndexPath *currentCellIndexPath1 = [tableView indexPathForCell:cell]; // Returns nil
NSIndexPath *currentCellIndexPath2 = [tableView indexPathForRowAtPoint:[cell center]]; // Returns nil
}
I call this updateCellTextView
method from textViewDidEndEditing
to save the textview's text in my model.
But I'm not able to get indexpath of cell.
How to get index path of a cell in this kind of scenarios i.e when cell is completely out of screen .
Upvotes: 0
Views: 709
Reputation: 831
If you want to get the indexpath
of cell in textViewDidEndEditing
you can use this code.
CGPoint point = [textView convertPoint:CGPointZero toView:self.tableview];
NSIndexPath *indexPath = [self.tableview indexPathForRowAtPoint:point];
Upvotes: 1
Reputation: 9503
Obviously you never get indexpath
of cell like this because this is only applicable in the UItableViewDelegates and UItableViewDatasource methods
.
As per I understand, Now you need to know on which UITextView
user is end texting. So one of the better approach is as below :
cellForRow
cell.textView.tag = indexPath.row
Here you have assigned tag to the textView
which is equivalent to its indexpath.row
. Now when ever you are talking about textView.tag
, it gives you the textView
of that particular cell and textView.tag
is that particular cell you have TextView
in - (BOOL)textViewShouldBeginEditing:(TextView *)textView
textView.tag // gives you indexPath.row of the textview where user ends editing
NOTE : If you have UITextView/UItextfield
in UItableView
, its value is always lost whenever your cell is out of screen. So to overwhelm this problem use dictionary save the each and every value of UITextView/UItextfield
in TextdidChange Method
.
Hope now you got it.
Upvotes: 0