2011-12-01 2 views
0

UITableview 그룹화 된 스타일의 두 섹션에 하나의 문제점이 있습니다. 첫 번째 섹션은 6 개의 행을, 두 번째 섹션은 3 개의 행을가집니다. 내가 테이블 뷰를 위아래로 스크롤하면 섹션 2의 마지막 행 내용이 첫 번째 섹션의 첫 번째 행에 추가되고 첫 번째 섹션의 첫 번째 행 컨텐트가 두 번째 섹션의 마지막 행에 추가됩니다. 내용을로드하기 위해 셀이 비어 있는지 여부를 가장 잘 확인하지만 디버깅 중에는 발생하지 않습니다. 내 코드는iPhone에서 UITableview 섹션을 덮어 쓰시겠습니까?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (cell == nil) 
     { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
      cell.backgroundColor = [UIColor whiteColor]; 

      if (indexPath.section == 0 && indexPath.row == 0) 
      { 
       UILabel *Nameabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 140, 30)]; 
       Nameabel.text = @"Name"; 
       Nameabel.font = [UIFont fontWithName:@"Helvetica" size:15]; 
       Nameabel.backgroundColor = [UIColor clearColor]; 
       Nameabel.font = [UIFont boldSystemFontOfSize:15]; 
       [cell.contentView addSubview:Nameabel]; 

       textField = [[UITextField alloc] initWithFrame:CGRectMake(145, 5, 150, 30)]; 
       textField.placeholder = @"Student Name"; 
       textField.borderStyle = UITextBorderStyleNone; 
       textField.font = [UIFont fontWithName:@"Helvetica" size:14]; 
       [cell.contentView addSubview:paymentCatTextField]; 

      } 
      else if (indexPath.section == 1 && indexPath.row == 0) 
      { 
       UILabel *RLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 140, 30)]; 
       RLabel.text = @"Class"; 
       RLabel.backgroundColor = [UIColor clearColor]; 
       RLabel.font = [UIFont fontWithName:@"Helvetica" size:15]; 
       RLabel.font = [UIFont boldSystemFontOfSize:15]; 
       [cell.contentView RLabel]; 

       RTextField = [[UITextField alloc] initWithFrame:CGRectMake(145, 5, 150, 30)]; 
       RTextField.placeholder = @"class Name"; 
       RTextField.borderStyle = UITextBorderStyleNone; 
       RTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation; 
       RTextField.font = [UIFont fontWithName:@"Helvetica" size:14]; 
       [cell.contentView addSubview:RTextField]; 
      } 
     } 
     return cell; 
    } 

이것은 샘플입니다. 이 방식으로 섹션 0은 6 개의 행을 가지며 섹션 1은 3 개의 행을가집니다. 내가 잘못하고있는 곳. 제발이 문제를 해결하는 데 도움이됩니다.

미리 감사드립니다.

+0

세포는주의 깊게 사과의 문서를 읽어야한다, 성능을 개선하기 위해 재사용 및 jQuery과에 대한 부분은 기본적와 중요성이다. –

답변

1
는 다음에 코드를 변경하고 다시 시도

:

{ 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.backgroundColor = [UIColor whiteColor]; 
    } 


    if (indexPath.section == 0 && indexPath.row == 0) { 
     //bla bla 
    } else if (indexPath.section == 1 && indexPath.row == 0) { 
     //bla bla 
    } 

    return cell; 
} 

이유는 당신이 셀을 재사용하지 않는 경우에만 셀 내용을 설정하는 것입니다. 그것은 대부분의 경우에 잘못입니다.

0

dasdom의 답변 외에도 레이블을 추가하기 전에 하위보기를 제거하는 것이 좋습니다.

if ([[cell.contentView subviews] count] > 0) { 
    UIView *labelToClear = [[cell.contentView subviews] objectAtIndex:0]; 
    [labelToClear removeFromSuperview]; 
    UIView *textToClear = [[cell.contentView subviews] objectAtIndex:1]; 
    [textToClear removeFromSuperview]; 
} 

및 autoreleasing 파단 :

UILabel *Nameabel = [[[UILabel alloc] initWithFrame:CGRectMake(5, 5, 140, 30)] autorelease]; 
UITextField *textField = [[[UITextField alloc] initWithFrame:CGRectMake(145, 5, 150, 30)] autorelease]; 
관련 문제