Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get position of cell or text field in this cell

I have a split view controller: view & tableview. In this table view I have custom cells with text field. I don't know how many cells will I have, so it generate automatically. And now I'm trying scroll to textfield, when it becomeFirstResponder. I've tried something like this:

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

300px - start position of my TableView. All seems alright, but textField.frame.origin.y always equal to 0 (like and bounds.origin.y btw).

I thought I can solve a problem if get position of cell, which textfield is active, and then replace textField.frame.origin.y on cell.frame.origin.y or something like this.

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

I forgot say that my tableviews scroll is disabled. I follow your advices and code examples and solve it like that:

- (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 know what? It's working! :-) But it create a new problem. When I begin editing textfield, scroller at first jump on top, and only then going to it correct position. When I write [scroller setContentOffset:focusOnTextField animated:NO]; and this problem disappear, but there is no smooth move of scroller. And this is bad to me :-) So how we can solve it?

like image 725
Vladislav Kovalyov Avatar asked Feb 16 '23 13:02

Vladislav Kovalyov


2 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 trigger that action when editing begins. Just do it using the cell...

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    // find the cell containing this text field
    UITableViewCell *cell = [self cellWithSubview:textField];

    // now scroll using that cell's index path as the target
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
like image 66
danh Avatar answered Feb 27 '23 12:02

danh


If you add the textfield in the UITableVieCell content view (added by default if you are using .xib) then you have to call something like textField.superview.superview and this will give you the parent cell. If you add the text field directly to the cell view then you have to textField.superview.

like image 24
danypata Avatar answered Feb 27 '23 11:02

danypata