2011-09-08 7 views
0

実際には、次のボタンと前のボタンを使って別のセルに移動し、各セルにテキストフィールドがあるので、次のボタンをクリックすると、次のセルに移動し、このセル参照を取得することでテキストフィールドを最初のレスポンダーですが、前のボタンをクリックしているときに参照が返されません。 iは次および前に使用していたコードがUITableViewCellにあるUITextFieldの参照を取得していますか?

- (IBAction)nextPrevious:(id)sender 
{ 
    NSIndexPath *indexPath ; 
    BOOL check = FALSE; 

    if([(UISegmentedControl *)sender selectedSegmentIndex] == 1){ 
     if(sectionCount>=0 && sectionCount<8){ 
      //for next button 
      check = TRUE; 
      sectionCount = sectionCount+1; 
      indexPath = [NSIndexPath indexPathForRow:0 inSection:sectionCount]; 
     } 
    }else{ 
     //for previous button 
     if(sectionCount>0 && sectionCount<=9){ 
      check = TRUE; 
      sectionCount = sectionCount-1; 

      indexPath = [NSIndexPath indexPathForRow:0 inSection:sectionCount]; 
     } 
    } 

    if(check == TRUE){ 
     //[registrationTbl reloadData]; 
     UITableViewCell *cell = [registrationTbl cellForRowAtIndexPath:indexPath]; 

     for(UIView *view in cell.contentView.subviews){ 
      if([view isKindOfClass:[UITextField class]]){ 
        [(UITextField *)view becomeFirstResponder]; 
        break; 
      } 
     } 

     [registrationTbl scrollToRowAtIndexPath:indexPath 
           atScrollPosition:UITableViewScrollPositionTop 
             animated:YES]; 


     // UITextField *field = (UITextField *) [cell.contentView viewWithTag:indexPath.section]; 
     // [field becomeFirstResponder]; 
    } 

下に与えられる任意の小さな提案がはるかに理解されるであろう。事前に感謝します

答えて

1

問題は、スクロールにあります。次の行の先頭にスクロールすると、前の行が削除され、最後に表示された行に再利用されます。つまり、cellForRowAtIndexPath:メソッドはおそらくセルが使用できないためnullを返します。

クイック&ダーティフィックスには、中にスクロールしたり、少しずれているため、セルが表示されます。 not-so-quick-nor-dirtyには、表をスクロールしてセルが見えるようにするプロシージャを作成し、スクロールが停止したときにテキストフィールドを最初のレスポンダとして設定します。

編集)もう少しこの最後のアプローチを説明してください。新しい変数NSIndexPath *indexPathEditingを追加したとします。デリゲート方法tableView:cellForRowAtIndexPath:を有するであろう:

if (indexPathEditing && indexPathEditing.row == indexPath.row && indexPathEditing.section == && indexPath.section) 
{ 
    // Retrieve the textfield with its tag. 
    [(UITextField*)[cell viewWithTag:<#Whatever#>] becomeFirstResponder]; 
    indexPathEditing = nil; 
} 

これは、indexPathEditingが設定され、そしてロードされている現在の行が表示されている場合、それは自動的にfirstResponderとして自身を設定することを意味します。

そして、例えば(あなたのnextPrevious:方法で)、すべてを行う必要がある:

indexPathEditing = [NSIndexPath indexPathForRow:0 inSection:sectionCount]; 

[registrationTbl scrollToRowAtIndexPath:indexPathEditing 
         atScrollPosition:UITableViewScrollPositionTop 
           animated:YES]; 
[registrationTbl reloadData]; 

行はtableView:cellForRowAtIndexPath:と呼ばれる、表示され、それが自動的にfirstResponderとして設定されます。

また、isKindOfClassでforを行うのではなく、タグ番号を設定してviewWithTag:でオブジェクトを取得する方が簡単であることに注意してください。

+0

ありがとうございました、ありがとうございました。それは私のために働いた。 –

関連する問題