2011-08-30 66 views
0

我幾乎成功地集成了自動UITableViewCellHeight thingy,但一件事情很奇怪。如果我運行它,它會顯示第三部分中的第一部分的文本(也是第一部分中的內容)。有想法該怎麼解決這個嗎?奇怪的結果與自動UITableViewCellHeight

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UILabel *label; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     label = [[[UILabel alloc] initWithFrame:CGRectZero]autorelease]; 
     [label setLineBreakMode:UILineBreakModeWordWrap]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     [label setMinimumFontSize:FONT_SIZE]; 
     [label setNumberOfLines:0]; 
     [label setFont:[UIFont systemFontOfSize:FONT_SIZE]]; 
     [label setTag:1]; 
     [[cell contentView] addSubview:label]; 

     if (indexPath.section == 0) {   
     NSString *text = @"abc."; 
     CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); 
     CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

     if (!label) 
      label = (UILabel*)[cell viewWithTag:1]; 

     [label setText:text]; 
      [label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))]; 
     } 

     else 
      if (indexPath.section == 1) { 
       NSString *text = @"def."; 
       CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); 
       CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

       if (!label) 
        label = (UILabel*)[cell viewWithTag:1]; 

       [label setText:text]; 
       [label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))]; 
      } 

      else 
       if (indexPath.section == 2) { 
        NSString *text = @"ghi."; 
        CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); 
        CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

        if (!label) 
         label = (UILabel*)[cell viewWithTag:1]; 

        [label setText:text]; 
        [label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))]; 
       } 
    } 
    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    if (indexPath.section == 0) { 
     NSString *text = @"abc."; 
     CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); 

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

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

     return height + (CELL_CONTENT_MARGIN * 2); 
    } 
    else 
     if (indexPath.section == 1) { 
      NSString *text = @"def."; 
      CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); 

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

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

      return height + (CELL_CONTENT_MARGIN * 2); 
     } 

     else 
      if (indexPath.section == 2) { 
       NSString *text = @"ghi."; 
       CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f); 

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

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

       return height + (CELL_CONTENT_MARGIN * 2); 
      } 
} 
+0

你真的* *應適當類的UITableViewCell,不重新設置所有這些內容的兩倍!然後將高度計算權建立到單元中,然後再調用。代碼少,清潔,少車。 – steipete

+0

是的,你顯然是對的。我只是試圖讓我的頭與此。 – mstottrop

回答

1

嘗試使用不同CellIdentifiers每個部分

static NSString *CellIdentifier; 
switch(indexPath.section){ 
    case 0: 
     CellIdentifier = @"first"; 
     break; 
    case 1: 
     CellIdentifier = @"second"; 
     break; 
    case 2: 
     CellIdentifier = @"third"; 
} 
+1

你是上帝!謝謝。 – mstottrop