UITableView自定义单元格显示奇怪的结果?

问题描述:

UITableView自定义单元格显示奇怪的结果?下面我有我的代码。一切都显示正常,直到我把足够的数据去掉最初的屏幕,然后开始疯狂?我的自定义单元格如下所示。UITableView自定义单元格显示奇怪的结果?

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; 
} 

当你离开屏幕时,旧的单元格排队到你的dequeReusableCell,老旧单元格的内容和属性必须仍然存在。将内容添加到单元格时,请确保清除以前的任何属性,以避免这些问题,或者关闭dequeReusableCell

+0

Noob:我怎么关闭deque? – jdog 2011-03-01 00:22:04

+0

好吧关闭它,它现在显示很好,但现在我失去了重用的好处? – jdog 2011-03-01 00:25:20

+0

我可以暂时删除contentView来做到这一点,或我需要手动清除每个属性? – jdog 2011-03-01 00:37:29

它看起来像一个单元复用的问题。为什么不关掉出队单元机制并查看问题是否消失。如果确实如此,请查看自定义单元中的代码 - 是否创建了多个字段标签的副本?它看起来这样。