2012-07-27 67 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]; 
     } 
    } 
}