簡體   English   中英

帶文本字段的UiTableView-出現鍵盤時滾動

[英]UiTableView with textfields - scroll when keyboard appears

我在tableview控制器中創建帶有文本字段的表單。 我試圖在為字段輸入值后按返回鍵來實現兩件事:

1)將光標移到下一個字段2)向上滾動表格視圖

我的代碼可以將光標移動到下一個字段,但是滾動部分不起作用。 你能告訴我我在這里想念的東西嗎? 我研究了有關堆棧溢出的類似問題,並遵循它們的建議,但仍然面臨問題。 感謝您的投入。

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    if ([textField isEqual:self.firstName]){
    //Move cursor to next field
    [self.lastName becomeFirstResponder];

    //scroll    
    id cellContainingFirstResponder = textField.superview.superview ;
    NSIndexPath *currentRowIndexPath = [self.signUpTable indexPathForCell:cellContainingFirstResponder];
    NSIndexPath *nextRowIndexPath = [NSIndexPath indexPathForRow:currentRowIndexPath.row+1 inSection:currentRowIndexPath.section];
    [self.signUpTable scrollToRowAtIndexPath:nextRowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

if ([textField isEqual:self.lastName]){
    //Move cursor to next field
    [self.emailId becomeFirstResponder];

    //scroll
    id cellContainingFirstResponder = textField.superview.superview ;
    NSIndexPath *currentRowIndexPath = [self.signUpTable indexPathForCell:cellContainingFirstResponder];
    NSIndexPath *nextRowIndexPath = [NSIndexPath indexPathForRow:currentRowIndexPath.row+1 inSection:currentRowIndexPath.section];
    [self.signUpTable scrollToRowAtIndexPath:nextRowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

if ([textField isEqual:self.emailId]){

    //Move cursor to next field
    [self.phoneNumber becomeFirstResponder];

    //scroll
    id cellContainingFirstResponder = textField.superview.superview ;
    NSIndexPath *currentRowIndexPath = [self.signUpTable indexPathForCell:cellContainingFirstResponder];
    NSIndexPath *nextRowIndexPath = [NSIndexPath indexPathForRow:currentRowIndexPath.row+1 inSection:currentRowIndexPath.section];
    [self.signUpTable scrollToRowAtIndexPath:nextRowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

if ([textField isEqual:self.phoneNumber]){

    //Move cursor to next field
    [self.password becomeFirstResponder];

    //scroll
    id cellContainingFirstResponder = textField.superview.superview ;
    NSIndexPath *currentRowIndexPath = [self.signUpTable indexPathForCell:cellContainingFirstResponder];
    NSIndexPath *nextRowIndexPath = [NSIndexPath indexPathForRow:currentRowIndexPath.row+1 inSection:currentRowIndexPath.section];
    [self.signUpTable scrollToRowAtIndexPath:nextRowIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

}

// This is the last field hence dismiss keyboard -> This part is working
if ([textField isEqual:self.password]){
    [textField resignFirstResponder];

}

return YES ;

}

為了根據文本字段的焦點向上滾動tableview,請嘗試以下操作:

- (void) textFieldDidBeginEditing:(UITextField *)textField {
    UITableViewCell *cell = (UITableViewCell*) [[textField superview] superview];
    [yourTableView scrollToRowAtIndexPath:[yourTableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];   
}

在這里,您可以使用UITableViewScrollPositionTop將滾動位置設置為頂部

您可能想知道為什么將兩個超級視圖用於textfield 一個用於UITableViewCellContentView ,另一個用於UITableViewCell以便您可以獲取表的當前/焦點單元格的實例。

它有效,我們已經在一個項目中使用了它

我使用viewcontroller作為表視圖的數據源。 我的解決方案:

  1. 將UITextField設置為自定義UITableViewCell的公共屬性。
  2. 在cellForRowAtIndexPath內部,將目標添加到UIControlEventEditingDidBegin控件事件的文本字段中:

     [((YourCustomCell *) cell).textfield addTarget:self action:@selector(textFieldDidBeginEditing:) forControlEvents:UIControlEventEditingDidBegin]; 
  3. 實現命名方法:找到文本字段的單元格並滾動到該單元格

     - (void)textFieldDidBeginEditing:(UITextField *)textField{ for ( NSIndexPath *ip in [self.tableview indexPathsForVisibleRows] ){ UITableViewCell * cell = [self.tableview cellForRowAtIndexPath:ip]; if ( [cell isKindOfClass:[YourCustomCell class]]){ if ( [textField isEqual:((YourCustomCell *)cell).textfield] ){ [self.tableview selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionTop]; [self setContinueButtonEnabled:YES]; } } } } 

您使用TKKeyboardAvoidingTableview,因此無需手動滾動tableview,當點擊光標時它會自動變色

您在選擇tableview中設置xib並設置這樣的自定義類

在此處輸入圖片說明

嘗試使用此代碼可能會對您有所幫助

  CGPoint pt;
    CGRect rc = [textField bounds];
    rc = [textField convertRect:rc toView:scrView];
    pt = rc.origin;
    pt.x = 0;
    pt.y =0;
    [scrView setContentOffset:pt animated:YES];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM