2011-02-28 71 views
1

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

回答

1

當你離開屏幕時,舊的單元格排隊到你的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

0

它看起來像一個單元複用的問題。爲什麼不關掉出隊單元機制並查看問題是否消失。如果確實如此,請查看自定義單元中的代碼 - 是否創建了多個字段標籤的副本?它看起來這樣。