2011-02-28 5 views
1

Cellule personnalisée UITableView présentant des résultats bizarres? J'ai mon code ci-dessous. Tout allait bien jusqu'à ce que je mette suffisamment de données pour sortir de l'écran initial, puis il a commencé à devenir fou? Ma cellule personnalisée est illustrée ci-dessous.Cellule personnalisée UITableView affichant des résultats bizarres?

enter image description here

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"customCell"; // This is identifier given in IB jason set this. 

    PostTableCustomCellController *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 

     NSLog(@"Cell created"); 

     NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"PostTableCustomCellController" owner:nil options:nil]; 

     for(id currentObject in nibObjects) 
     { 
      if([currentObject isKindOfClass:[PostTableCustomCellController class]]) 
      { 
       cell = (PostTableCustomCellController *)currentObject; 
      } 
     } 
    } 

    Post *post = [postsArray objectAtIndex:indexPath.row]; 

    cell.authorName.text = post.author; 
    cell.deadline.text = post.deadline; 
    cell.description.text = post.description; 


    Post *myPost = [postsArray objectAtIndex:[indexPath row]]; 
    NSString *text = myPost.description; 

    // Configure the cell...    
    // [[cell authorName] setText:@"1 day"]; 
    // [[cell distance] setText:@"Austin, TX"]; 
    // [[cell description] setText:@"dd" ]; 


    // Might can remove these 
    UILabel *locationLabel = (UILabel *) [cell distance]; 
    UILabel *postTextView = (UILabel *) [cell description]; 



    //CGSize maximumLabelSize = CGSizeMake(254,88.0f); 



    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 88.0f); 

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

    CGFloat height = MAX(size.height, 35.0f); 

    CGFloat realHeight = height + 36.0f - (10); 


    CGSize expectedLabelSize = [text sizeWithFont:postTextView.font 
           constrainedToSize:constraint 
            lineBreakMode:postTextView.lineBreakMode]; 
    CGRect newFrame = postTextView.frame; 
    newFrame.size.height = expectedLabelSize.height; 
    postTextView.frame = newFrame; 

    [[cell description] sizeToFit]; 

    CGRect locationRect = locationLabel.frame; // calls the getter 
    locationRect.origin.y = realHeight; 

    /* CGFloat locRectHeight = postTextView.bounds.size.height; */ 
    /* locationRect.origin.y = cell.bounds.size.height; */ 
    locationLabel.frame = locationRect; 

    //[[cell authorName] setText:@"jgervin"]; 
    [[cell viewForBackground] sizeToFit]; 


    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    Post *myPost = [postsArray objectAtIndex:[indexPath row]]; 
    NSString *text = myPost.description; 

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 88.0f); 

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

    CGFloat height = MAX(size.height, 35.0f); 

    return height + (CELL_CONTENT_MARGIN * 2) + 36.0f; 
} 

Répondre

1

Vous appelez les anciennes cellules lorsqu'elles sortent de l'écran sur votre dequeReusableCell, et le contenu et les propriétés de l'ancienne cellule doivent toujours être présents. Lorsque vous ajoutez du contenu à une cellule, assurez-vous d'effacer les propriétés précédentes afin d'éviter ces problèmes, ou désactivez simplement dequeReusableCell.

+0

Noob: comment arrêter une deque? – jdog

+0

Ok éteignez-le et ça se voit bien maintenant, mais maintenant je perds les avantages de la réutilisation? – jdog

+0

Puis-je exclure contentView pour le faire en même temps ou dois-je effacer chaque propriété manuellement? – jdog

0

Il ressemble à un problème de réutilisation des cellules. Pourquoi ne pas désactiver le mécanisme de la cellule dequeue-file et voir si le problème disparaît. Si c'est le cas, jetez un oeil au code dans votre cellule personnalisée - créez-vous plusieurs copies des étiquettes de champ? Ça ressemble à ça.

Questions connexes