2013-02-27 96 views
1

我已經創建了一個表格,其大小根據將填充它們的內容的數量動態變化。我遇到的問題是,當我向下滾動然後再次返回時第一個單元格添加了不應該在那裏的內容。UITableViewCell在滾動後顯示第一個單元格中的最後一個單元格。

Here是一個視頻顯示我的問題。

這裏是我用來嘗試和實現這個的代碼。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 


    static NSString *CellIdentifier = @"Cell"; 
    //NotificationCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    noteBody = [Noifications objectAtIndex:indexPath.row]; 
    yPosition = 10; 
    NSLog(@"noteBody: %@",noteBody); 

    double titleHeight = 0; 
    NSUInteger titleLenght = [noteBody objectForKey:@"title"]; 

    if (titleLenght < 30)titleHeight = 25; 
    else if(titleLenght < 60)titleHeight = 50; 
    else if(titleLenght < 90)titleHeight = 75; 
    else if(titleLenght < 120)titleHeight = 100; 
    else if(titleLenght < 150)titleHeight = 125; 
    else if(titleLenght < 180)titleHeight = 150; 
    else titleHeight = 200; 


    UILabel *Title = [[UILabel alloc]initWithFrame:CGRectMake(10, yPosition, self.view.frame.size.width-30, titleHeight)]; 
    Title.text = [noteBody objectForKey:@"title"]; 
    Title.font = [UIFont boldSystemFontOfSize:20]; 
    Title.textAlignment = NSTextAlignmentCenter; 
    Title.numberOfLines = 0; 
    [Title sizeToFit]; 
    yPosition += Title.frame.size.height; 
    [cell addSubview:Title]; 

    UILabel *Date = [[UILabel alloc]initWithFrame:CGRectMake(10, yPosition, self.view.frame.size.width-30, 25)]; 
    Date.text = [noteBody objectForKey:@"date"]; 
    Date.numberOfLines = 0; 
    [Title sizeToFit]; 
    yPosition += Date.frame.size.height; 
    [cell addSubview:Date]; 

    double bodyHeight = 0; 
    NSUInteger bodyLenght = [noteBody objectForKey:@"title"]; 

    if (bodyLenght < 30)titleHeight = 25; 
    else if(bodyHeight < 60)titleHeight = 50; 
    else if(bodyHeight < 90)titleHeight = 75; 
    else if(bodyHeight < 120)titleHeight = 100; 
    else if(bodyHeight < 150)titleHeight = 125; 
    else if(bodyHeight < 180)titleHeight = 150; 
    else if(bodyHeight < 210)titleHeight = 175; 
    else if(bodyHeight < 240)titleHeight = 200; 
    else if(bodyHeight < 270)titleHeight = 225; 
    else if(bodyHeight < 300)titleHeight = 250; 
    else if(bodyHeight < 330)titleHeight = 275; 
    else if(bodyHeight < 360)titleHeight = 300; 
    else if(bodyHeight < 390)titleHeight = 325; 
    else bodyHeight = 350; 


    UILabel *Body = [[UILabel alloc]initWithFrame:CGRectMake(10, yPosition, self.view.frame.size.width-30, bodyHeight)]; 
    Body.text = [noteBody objectForKey:@"body"]; 
    Body.numberOfLines = 0; 
    [Body sizeToFit]; 
    yPosition += Body.frame.size.height; 
    [cell addSubview:Body]; 



    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    noteBodyCellHeight = [Noifications objectAtIndex:indexPath.row]; 

    NSLog(@"cellHeights: %@",cellHeights); 

    NSString *title = [noteBodyCellHeight objectForKey:@"title"]; 
    NSString *date = [noteBodyCellHeight objectForKey:@"date"]; 
    NSString *body = [noteBodyCellHeight objectForKey:@"body"]; 

    CGRect frame = [UIScreen mainScreen].bounds; 
    CGFloat width = frame.size.width; 

    int section = indexPath.section; 


    CGSize title_size = {0, 0}; 
    CGSize date_size = {0, 0}; 
    CGSize body_size = {0, 0}; 

    if (title && [title isEqualToString:@""] == NO) { 
     title_size = [title sizeWithFont:[UIFont systemFontOfSize:22.0] 
           constrainedToSize:CGSizeMake(width, 4000) 
            lineBreakMode:UILineBreakModeWordWrap]; 
    } 

    if (date && [date isEqualToString:@""] == NO) { 
     date_size = [date sizeWithFont:[UIFont systemFontOfSize:18.0] 
         constrainedToSize:CGSizeMake(width, 4000) 
          lineBreakMode:UILineBreakModeWordWrap]; 
    } 

    if (body && [body isEqualToString:@""] == NO) { 
     body_size = [body sizeWithFont:[UIFont systemFontOfSize:18.0] 
           constrainedToSize:CGSizeMake(width, 4000) 
            lineBreakMode:UILineBreakModeWordWrap]; 
    } 

    CGFloat title_height = title_size.height; 
    CGFloat date_height = date_size.height; 
    CGFloat body_height = body_size.height; 

    //CGFloat content_size = Body.frame.size.height + Title.frame.size.height + Date.frame.size.height; 
    CGFloat content_size = title_height + date_height + body_height +20; 
    NSLog(@"content_size: %f",content_size); 
    CGFloat height; 

    switch (section) { 

     case 0: 
      height = content_size; 
      break; 

      //Just in case 
     default: 
      height = 44.0; 
      break; 

    } 

    return height; 


} 

有誰能告訴我我哪裏出了問題,並指出我該如何解決這個問題?

+0

表格單元格被重新使用,因此所有內容都是「髒的」並需要設置/清除。換句話說,如果您將某物添加到單元格並重新使用它,該物品在重新使用時仍然會在那裏! – DBD 2013-02-27 16:20:40

回答

3

嘗試使用自定義單元格類或刪除重用中的單元格現有子視圖。

基本上每次調用cellForRow ..子視圖被添加到單元中,即使它正在被重用。

if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

添加此上述

for(UIView *view in cell.contentView.subviews) 
{ 
    [view removeFromSuperView]; 
} 

後也改變cell addSubviewcell.contentView addSubview

+0

非常感謝:)我工作過。 – Will 2013-02-27 16:30:06

1

你可能想你的三個子視圖添加到該小區只有一次,給每一個獨特的標籤,即後在if塊中初始化它。之後,您可以在單元格上使用-viewWithTag:來引用標籤並設置其大小和內容。

相關問題