2013-05-14 71 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