2011-12-12 2 views
1

테이블 뷰 성능을 향상시키기 위해 일부 코드를 다시 작성하고 있습니다. 나는 dequeueReusableCellWithIdentifier를 구현하려고하는데 많은 행운이 없다.재사용을 위해 tableview 셀을 지우는 방법

셀이 화면에서 벗어나서 다시 켜지면 내용이 복제됩니다 (화면이 꺼지고 화면으로 돌아갈 때마다). 세포의 내용물은 다른 세포에도 나타나서는 안됩니다.

세포가 재사용 될 때 콘텐츠가 삭제되어야한다고 생각하지만 이해할 수 없습니다.

세포 자체가 연속으로 4 개 CUSTOM 이미지 버튼과 4 개 레이블 구성 :

[ ] [ ] [ ] [ ] 
abc abc abc abc 

일부 세포는 모든 이미지/라벨을 채워있을 수 있습니다, 다른 사람은있을 수 있습니다 1 또는 2

아래

코드 ..

- (UITableViewCell *)tableView:(UITableView *)thetableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //NSString *CellIdentifier = [NSString stringWithFormat:@"Cell",indexPath.section]; 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    else 
    { 
     //Tried this to clear old contents but doesn't make a difference 
     for (HomeScreenButton* b in cell.subviews) 
     { 
      NSLog(@"RemovedOldButton"); 

      b = nil; 
     } 
     for (UILabel* b in cell.subviews) 
     { 
      NSLog(@"RemovedOldLabel"); 

      b = nil; 
     } 
    } 
    cell.accessoryType = UITableViewCellAccessoryNone; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

    int section = indexPath.section; 


    NSMutableArray *sectionItems = [sections objectAtIndex:section]; //Gets the array of objects for this section 
    if (sectionItems.count == 0) return cell; 

    LepidEntity * le = [sectionItems objectAtIndex:0];// Gets the object for this section (family/group etc.) 


    if (([le.type isEqualToString:@"family"]) || ([le.type isEqualToString:@"group"])) 
    { 
     NSMutableArray * tmpAnn; 
     int annCount = 0; 
     //Get annotations for this family/group 
     if ([le.type isEqualToString:@"family"]) 
     { 
      Family * family = (Family *)le; 
      tmpAnn = [[NSMutableArray alloc]initWithArray:family.annotations]; 
     } 
     else 
     { 
      Group * group = (Group *)le; 
      tmpAnn = [[NSMutableArray alloc]initWithArray:group.annotations]; 

     } 
     annCount = tmpAnn.count; 
     //We want to start at the last item in the array. indexPath.row*4. First row would be 0 then 4, then 8 etc. 
     for (int i = indexPath.row*4;i < tmpAnn.count;i++) 
     { 
      Annotation * theAnnotation = [tmpAnn objectAtIndex:i]; 
      CGRect bRect = CGRectMake(i*80, 5, 80, 80); 
      HomeScreenButton *button = [self getResultsViewButton:bRect imagePath:theAnnotation.image cellForRowAtIndexPath:indexPath i:i entity:theAnnotation]; 
      [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
      [cell.contentView addSubview:button]; 
      [button release]; 
      button = nil; 
      //Get and configure the label 
      CGRect lRect = CGRectMake(i*80, 80, 100, 16); 
      UILabel *label = [self getResultsViewLabel:lRect text:theAnnotation.name cellForRowAtIndexPath:indexPath i:i]; 
      [cell.contentView addSubview:label]; 
      [label release]; 
      label = nil; 
     } 
    } 
    else 
    { 
     Species * s; 
    } 

    return cell;} 

-(HomeScreenButton *) getResultsViewButton:(CGRect)rect imagePath:(NSString *)image 

    cellForRowAtIndexPath:(NSIndexPath *)indexPath i:(int)i entity:(LepidEntity *)entity 
    { 
     HomeScreenButton *button=[[HomeScreenButton alloc] initWithFrame:rect]; 

     image = [image stringByReplacingOccurrencesOfString:@"Images/" withString:@"Thumbs/"]; 

     UIImage *buttonImageNormal=[UIImage imageWithContentsOfFile:GetFullPath(image)];//imageNamed:image]; 
     [button setBackgroundImage:buttonImageNormal forState:UIControlStateNormal]; 
     [button setContentMode:UIViewContentModeCenter]; 
     NSString *tagValue = [NSString stringWithFormat:@"%d%d", indexPath.section+1, i]; 
     button.tag = [tagValue intValue]; 
     button.entity = entity; 
     return button; 
    } 
+0

위의 그림에서 알 수 있듯이 레이블이 오른쪽이 아닌 이미지 아래에 있음을 유의하십시오. 왜 그런 짓을했는지 모르겠다. – Dann

+0

자신을 고칠 수있다. 변경 : [cell.contentView addSubview : button] to [cell addSubview : button] – Dann

답변

1

[b removeFromSuperview]으로 설정하는 대신 nil로 설정해야합니다.

+0

도움 주셔서 감사합니다. 거의! [b removeFromSuperView]를 사용하면 해당 셀이 다시 나타나지 않습니다. (다른 셀 또는 원래의 셀로 다시 스크롤 한 후) – Dann

+0

아니요, 죄송합니다. 나가 말한대로, 더 이상 그 (것)들을 중복하지 않는다 그러나 나가 아래로 내리는 경우에, 아무것도 다른 세포에서 나타나지 않는다. 그리고 첫 번째 페이지로 스크롤하면 빈 셀이된다. – Dann

+0

'getResultsViewButton'은 어떻게 생겼는가? – tobiasbayer

0

첫 번째 else 조건을 제거하십시오 ..

+0

이렇게하면 동일한 동작이 발생합니다. 또는 b = nil에서 [b removeFromSuperView]로 변경하면 아무 것도 나타나지 않습니다. – Dann

관련 문제