2013-07-18 37 views
0

我正在tableview控制器中創建一個帶有textfields的窗體。我想,當返回鍵的字段中輸入值後按在這裏實現兩件事情:帶文本區域的UiTableView - 出現鍵盤時滾動

1)將光標移動到下一個字段 2)滾動的tableview了

我的代碼正在爲移動光標移到下一個字段但滾動部分不起作用。你能不能讓我知道我在這裏失蹤了什麼。我研究了堆棧溢出的類似問題,並且從他們那裏得到了一些建議,但仍然面臨着問題。欣賞你的投入。

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

} 
+0

那是因爲你的ContentSize不夠大,嘗試並增加你的UITableView ContentSize。 – Bonnie

回答

1

您使用TKKeyboardAvoidingTableview所以沒必要碼的手動滾動tableview中時標抽頭

設置在選擇的tableview廈門國際銀行,並設置自定義類這樣

enter image description here

+0

很好的工作更好的解決方案,不需要浪費時間...... –

1

嘗試將其自動scoll使用此代碼它可能會幫助你

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

滾動的實現代碼如下起來按文本框的焦點試試這個:使用UITableViewScrollPositionTop

你可能想知道爲什麼是兩個superviews用於textfield

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

在這裏你可以設置滾動位置上方。一個用於UITableViewCellContentView,另一個用於UITableViewCell,以便您可以抓取表格當前/重點單元格的實例。

它的工作原理,我們已經使用了這個在我們的項目

+1

在iOS7上有一個額外的視圖在UITableViewCell(據我所知,它是爲滑動手勢和刪除按鈕)。所以,你的解決方案將無法長期運作。我建議找到一種方法來查找視圖控制器上的textfields所有者單元格(例如遍歷單元格和比較textfields) –

+0

我認爲這不是上述問題的適當解決方案... –

2

的一個我用的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]; 
          } 
         } 
        } 
    } 
    
+1

有用的方法! –

相關問題