2011-09-08 6 views
0

실제로 나는 다른 셀로 이동하기 위해 다음 및 이전 버튼을 사용하고 있으며 각 셀에는 텍스트 필드가 있으므로 다음 버튼을 누르면 다음 셀로 이동하고이 셀 참조를 가져 와서 텍스트 필드를 만들 수 있습니다. 첫 번째 응답자하지만 이전 버튼을 클릭하면 참조가 반환됩니다. 난 다음 및 이전을 위해 사용하고 코드는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 자체를 설정할 것을 의미한다. ,

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

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

행이 나타납니다라는 tableView:cellForRowAtIndexPath:, 그것은 자동으로 firstResponder로 설정 얻을 것이다 :

그런 다음, (당신의 nextPrevious: 방법) 예를 들어, 당신이 할 필요가 입니다 .

isKindOfClass으로 for를 사용하는 대신 태그 번호를 설정하고 viewWithTag:과 함께 개체를 검색하는 것이 더 쉽습니다.이 예제에서는 이것을 통합했습니다.

+0

고맙습니다, 감사합니다. 대단히 감사합니다. 그것은 나를 위해 일했습니다. –

관련 문제