2013-05-14 68 views
4

我有一个分割视图控制器:查看& tableview。在这个表格视图中,我有自定义单元格和文本字段。我不知道我会有多少个细胞,所以它会自动生成。现在我试图滚动到文本框,当它成为FirstResponder。我试过这样的事情:获取单元格或文本字段在此单元格中的位置

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

300px - 我的TableView的开始位置。所有似乎都没问题,但textField.frame.origin.y总是等于0(像和bounds.origin.y顺便说一句)。

我以为我可以解决一个问题,如果获取单元格的位置,哪个文本字段是活动的,然后替换textField.frame.origin.ycell.frame.origin.y或类似的东西。

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

我忘了说,我的tableviews滚动被禁用。我遵循你的建议和代码示例,并像这样解决:

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

而且知道什么?它正在工作! :-)但它造成了一个新问题。当我开始编辑文本框时,滚动条首先跳到顶部,然后才转到正确的位置。当我写[scroller setContentOffset:focusOnTextField animated:NO];这个问题消失了,但是没有平滑的滚动条。这对我来说很糟糕:-)那么我们如何解决它呢?

回答

5

下面是如何滚动到包含文本字段的单元格...

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

你的想法是正确的触发编辑开始时的动作。只要用细胞做...

- (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

如果您在UITableVieCell内容视图中添加文本框(如果您使用的是.xib,则默认添加),那么您必须调用类似textField.superview.superview的东西,这会为您提供父级单元格。如果您将文本字段直接添加到单元格视图,那么您必须textField.superview

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

添加以下类别的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 
相关问题