Get cell position or text field in this cell

I have a split view controller: view and tableview. In this table view, I have custom cells with a text box. I don’t know how many cells I will have, so it will be generated automatically. And now I'm trying to scroll the text box when it becomes FirstResponder. I tried something like this:

-(void) textFieldDidBeginEditing:(UITextField *)textField { CGPoint focusOnTextField = CGPointMake(0, 300 + textField.frame.origin.y); [scroller setContentOffset: focusOnTextField animated: YES]; } 

300px is the starting position of my TableView. Everything looks good, but textField.frame.origin.y always 0 (for example, and bounds.origin.y btw).

I thought I could solve the problem if I get the position of the cell whose text field is active, and then replace textField.frame.origin.y with cell.frame.origin.y or something like that.

==================================================== ==================

I forgot to say that my scroll table is disabled. I follow your tips and code examples and solve them like this:

 - (UITableViewCell *)cellWithSubview:(UIView *)subview { while (subview && ![subview isKindOfClass:[UITableViewCell self]]) subview = subview.superview; return (UITableViewCell *)subview; } - (void)textFieldDidBeginEditing:(UITextField *)textField { UITableViewCell *activeCell = [self cellWithSubview:textField]; float offsetValueY = 200 + activeCell.origin.y; CGPoint focusOnTextField = CGPointMake(0, offsetValueY); [scroller setContentOffset:focusOnTextField animated:YES]; } 

And you know what? It works! :-) But this creates a new problem. When I start editing a text field, the scroller first jumps from above, and only then comes to the right position. When I write [scroller setContentOffset:focusOnTextField animated:NO]; , and this problem disappears, but there is no smooth movement of the scroller. And this is bad for me :-) So how can we solve this?

+4
source share
3 answers

Here's how to scroll to a cell containing a text field ...

 // find the cell containing a subview. this works independently of how cells // have been constructed. - (UITableViewCell *)cellWithSubview:(UIView *)subview { while (subview && ![subview isKindOfClass:[UITableViewCell self]]) subview = subview.superview; return (UITableViewCell *)subview; } 

Your idea is correct to initiate this action at the beginning of editing. Just do it with a cell ...

 - (void)textFieldDidBeginEditing:(UITextField *)textField { // find the cell containing this text field UITableViewCell *cell = [self cellWithSubview:textField]; // now scroll using that cell index path as the target NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; } 
+5
source

If you add a text box to the content view of UITableVieCell (added by default if you use .xib), you should call something like textField.superview.superview and this will give you the parent cell. If you add a text field directly to the cell view, you must textField.superview .

+2
source
 [tableView scrollToRowContainingComponent:textField atScrollPosition: UITableViewScrollPositionMiddle animated:YES]; 

after adding the following category to the UITableView:

 @implementation UITableView (MyCategory) -(NSIndexPath*)indexPathOfCellComponent:(UIView*)component { if([component isDescendantOfView:self] && component != self) { CGPoint point = [component.superview convertPoint:component.center toView:self]; return [self indexPathForRowAtPoint:point]; } else { return nil; } } -(void)scrollToRowContainingComponent:(UIView*)component atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated { NSIndexPath *indexPath = [self indexPathOfCellComponent:component]; if(indexPath) { [self scrollToRowAtIndexPath:indexPath atScrollPosition: scrollPosition animated:animated]; } } @end 
+1
source

Source: https://habr.com/ru/post/1480751/


All Articles