Get position of cell or text field in this cell

谁说胖子不能爱 提交于 2020-01-03 18:38:27

问题


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?


回答1:


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];
}



回答2:


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.




回答3:


[tableView scrollToRowContainingComponent:textField atScrollPosition: UITableViewScrollPositionMiddle animated:YES];

after adding the following category to 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


来源:https://stackoverflow.com/questions/16547549/get-position-of-cell-or-text-field-in-this-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!