2012-07-27 25 views
0

我有分組的TableView用5段和類型爲50個自定義細胞的總的自定義:的UITextField沒有得到光標時的UITableView scrolles到不可見的細胞

Label  | XXX 

其中XXX可以的UITextField,UITextView的, UIButton,UILabel。單元格在tableView中有混合順序。

enter image description here

要通過文本框的瀏覽& textViews我加入我的鍵盤上的下一個/上一個按鈕。

enter image description here

問題是當我選擇txtField和單擊下一步或上一頁按鈕,表滾動到所需的細胞位置,但txtField沒有得到光標。爲了與文本框/的TextView得到小區i使用:

nextCell = [self tableView:_myTableView 
     cellForRowAtIndexPath:[NSIndexPath indexPathForRow:curRow inSection:curSec]]; 

我需要通過的tableView的所有細胞,不僅通過可見的人進行搜索。

正如我測試,問題是在這行代碼,因爲如果我打電話:

nextCell = [_myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:curRow inSection:curSec]]; 

和所述細胞是可見的,則txtField得到光標。但是,如果我需要從單元格(section = 1,row = 6)跳轉到不可見的單元格(section = 0,row = 3),我會得到nil。

我的按鈕功能的全碼:

-(void) previousTextField:(id)sender 
{ 
    int curTagInd = _editingTextElement.tag; 

    NSIndexPath *curInd = [self getRowIndexOf:_editingTextElement]; 
    int curSec = curInd.section; 
    int curRow = curInd.row; 

    id nextView = nil; 
    UITableViewCell *nextCell = nil; 

    do {   
    curRow--; 

    if (curRow >=0) { 
     nextCell = [self tableView:_myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:curRow inSection:curSec]];//[_myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:--curRow inSection:curInd.section]]; 
     curTagInd--; 
     if ([nextCell isKindOfClass:[CustomCell class]] || [nextCell isKindOfClass:[CustomLongTextCell class]]) { 
      do { 
       nextView = [nextCell viewWithTag:curTagInd]; 
       if ([nextView isEnabled]) { 
        nextCell = nil; 
        _editingIndexPath = [NSIndexPath indexPathForRow:curRow inSection:curSec]; 
        break; 
       }else{ 
        break; 
       } 
      } while (nextView != nil && ((![nextView isKindOfClass:[UITextField class]] && ![nextView isKindOfClass:[UITextView class]]) || ![nextView isEnabled])); 
     } 
     } 
     else if(curSec > 0){ //got to previous section 
     curSec--; 
     curRow = [self getNumberOfRowsInSection:curSec]; 
     curTagInd = [self getNumberOfRowsInSection:curSec]+1; 
     } 
     else{ 
     nextCell = nil; 
     } 

    } while (nextCell != nil); 

    if (nextView != nil) { 
     [self.myTableView scrollToRowAtIndexPath:_editingIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 

     UITextField *textField = (UITextField *)nextView; 
     [textField becomeFirstResponder]; 
    } 
} 

使用此代碼我進去方法textFieldShouldBeginEditing:,但不是在textFieldDidBeginEditing:

回答

0

SOLUTION:

的問題是,該表纔開始滾動在函數previousTextField:完成後,因此在表滾動到所需位置之前調用了[textField becomeFirstResponder];。由於所需單元在屏幕上仍然不可見,因此委託方法未調用。

爲了避免這種情況,我增加計時器表滾動的行之後:

NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 0.2 
               target: self 
              selector:@selector(doneScrolling) 
              userInfo: nil repeats:NO]; 

和類似的定義doneScrolling

-(void)doneScrolling 
{ 
    NSLog(@"done scrolling"); 
    [self tableView:self.myTableView didSelectRowAtIndexPath:self.editingIndexPath]; 
} 

其中:

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:NO]; 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    UIView *nextView = [cell viewWithTag:indexPath.row+1]; 
    if ([nextView isKindOfClass:[UITextView class]] || [nextView isKindOfClass:[UITextField class]]) { 
     if ([nextView isEnabled]) { 
      UITextField *textField = (UITextField *)nextView; 
      [textField becomeFirstResponder]; 
     } 
    } 
} 
相關問題