2013-11-14 2 views
0

나는 20 행의 간단한 tableView를 가지고 있습니다. 하위 클래스 사용자 정의 UITableview 셀을 만들고 cellforRowAtIndex에서 다른 모든 3 행에 한 번 셀에 텍스트 필드를 추가합니다. 위아래로 스크롤 할 때 텍스트 필드가 잘못된 행에 표시됩니다. 참고,이 UItextfield 내 사용자 지정 셀의 일부를 만들 수 없습니다,이 확인란, 라디오 버튼을 수 있지만 단순화하기 때문에 UITextfield 선택한 ... 내가 뭘 잘못하고있어?UITableViewCell 재사용 가능성

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
    { 
    static NSString *CellIdentifier = @"TestCellIdentifier"; 
    testCell *cell = (testCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
if(!cell) 
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
else{ 

    //HMMM I also tried removing it before adding it- it doesn't work neither 
    for(UIView *v in cell.subviews){ 
     if(v.tag == 999){ 
      [v removeFromSuperview]; 
     } 
    } 

    //add UItextField to row if it's divisible by 3 
    if(indexPath.row %3 ==0){ 

     UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(400, 10, 300, 30)]; 
     textField.borderStyle = UITextBorderStyleRoundedRect; 
     textField.font = [UIFont systemFontOfSize:15]; 
     textField.placeholder = [NSString stringWithFormat:@"%d",indexPath.row]; 
     textField.autocorrectionType = UITextAutocorrectionTypeNo; 
     textField.keyboardType = UIKeyboardTypeDefault; 
     textField.returnKeyType = UIReturnKeyDone; 
     textField.clearButtonMode = UITextFieldViewModeWhileEditing; 
     textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
     textField.tag = 999; 

     [cell addSubview:textField]; 
    } 
} 


cell.textLabel.text = [NSString stringWithFormat:@"%d",indexPath.row]; 


return cell; 
} 

답변

0

재사용 가능성을 사용하지 않습니까? 이 장면에서 재사용을 사용하지 않겠습니다.

+0

예를, 나는 그것을 효율적 아니라고 단지 걱정했다, 그것은 재사용 가능한 세포없이 작업했다,하지만 내 테이블 25 줄 이상되지 않을 것입니다. 그래서 나는 괜찮다고 생각합니다. 감사합니다 – IronMan1980

0

세포를 재사용하는 것은 좋은 일이며 수행 할 수 있어야합니다.

는 오프 스크린 전환 될 때 모니터가 위임 프로토콜 방식으로, 재사용 대기 전에, 셀에서 텍스트 필드를 제거 고려할 수

:

– tableView:didEndDisplayingCell:forRowAtIndexPath: 

행 번호를 알고 당신이 알고있는 것에 대한 여부 텍스트 필드를 제거하십시오. 코드 확인을 보이는 언뜻보기에

, 그래서 나는 약간의 테스트 프로젝트를했다 :


편집

는 설명을 추가 할 수 있습니다. 원래 코드의 문제점은 잘못된 "보기"에 텍스트 필드를 추가한다는 것입니다. UITableViewCells에는주의가 필요한 구조가 있습니다. UITableViewCell contentView 속성에 대한 설명서를 살펴보십시오. 당신은 단순히 추가 뷰를 추가하여 세포를 사용자 정의하려면 그들이에와 편집 모드 중 셀 전환으로 적절하게 을 배치 할 수 있도록

, 당신은 내용보기에 추가해야합니다 : 그것은 부분적으로 말한다 .

그래서 코드가 셀의있는 contentView의 서브 뷰에 추가하고 열거한다 :

for(UIView *v in cell.contentView.subviews){ 
     if(v.tag == 999){ 
      [v removeFromSuperview]; 
     } 
    } 
... 
    textField.tag = 999; 
    [cell.contentView addSubview:textField]; 
+0

나는 이것을 시험해 보지 않았습니다. – IronMan1980

+0

셀에서 텍스트 필드를 다시 만들지 못하게했습니다. 나는 (UIView * v를 cell.subviews에 넣는다) { if (v.tag == 999) { [v removeFromSuperview]; } } "didEndDisplayingCell"에서이 "if"문을 입력 할 수 없었습니다. – IronMan1980

+0

내 대답을 편집하여 문제에 대한 설명을 제공했습니다. –

관련 문제