2012-12-01 178 views
1

我需要根據它的內容設置tableViewCell的高度。我在tableViewCell中有一些動態高度的標籤,標籤中的行數不是恆定的。有時候,如果一個標籤的文本是null,那麼其他標籤可能佔據其位置。所以我需要的是tableViewCell應該根據內容高度autoresizeTableViewCell根據xcode中的內容自動調整其高度?

-(void)viewDidLoad 
{ 
self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,150,327,[array count]*205) style:UITableViewStylePlain]; 
       self.tableView.delegate=self; 
       self.tableView.dataSource=self; 
       self.tableView.scrollEnabled = NO; 
       [self.view addSubview:self.tableView]; 

       float fscrview = 150 + self.tableView.frame.size.height + 20; 
       testscroll.contentSize=CGSizeMake(320, fscrview); 
} 


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

     return [array count]; 
    } 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *[email protected]"Cell"; 

     UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

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


      UILabel *name1 = [[UILabel alloc] initWithFrame:CGRectZero]; 
      name1.tag = 111; 
      name1.backgroundColor = [UIColor clearColor]; 
      [name1 setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14]]; 
      [name1 setLineBreakMode:UILineBreakModeWordWrap]; 
      [cell addSubview:name1]; 

      UILabel *code = [[UILabel alloc] initWithFrame:CGRectZero]; 
      codetype.tag = 112; 
      codetype.backgroundColor = [UIColor clearColor]; 
      [codetype setFont:[UIFont fontWithName:@"Helvetica" size:12]]; 
      [codetype setLineBreakMode:UILineBreakModeWordWrap]; 
      [cell addSubview:codetype]; 

      line1 = [[UILabel alloc] initWithFrame:CGRectZero]; 
      line1.tag = 113; 
      line1.backgroundColor = [UIColor clearColor]; 
      [line1 setFont:[UIFont fontWithName:@"Helvetica" size:12]]; 
      [line1 setLineBreakMode:UILineBreakModeWordWrap]; 
      [cell addSubview:line1]; 


      line3 = [[UILabel alloc] initWithFrame:CGRectZero]; 
      line3.tag = 114; 
      line3.backgroundColor = [UIColor clearColor]; 
      [line3 setFont:[UIFont fontWithName:@"Helvetica" size:12]]; 
      [line3 setLineBreakMode:UILineBreakModeWordWrap]; 
      [cell addSubview:line3]; 

      city = [[UILabel alloc] initWithFrame:CGRectZero]; 
      city.tag = 115; 
      city.backgroundColor = [UIColor clearColor]; 
      [city setFont:[UIFont fontWithName:@"Helvetica" size:12]]; 
      [city setLineBreakMode:UILineBreakModeWordWrap]; 
      [cell addSubview:city]; 



     } 

     cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton; 
     NSMutableDictionary *d =[[NSMutableDictionary alloc]initWithDictionary: [arr2 objectAtIndex:indexPath.row]]; 


     NSString *name2 = [d objectForKey:@"Name"]; 
     CGSize constraint1 = CGSizeMake(175, 2000.0f); 
     CGSize size1 = [name2 sizeWithFont: [UIFont fontWithName:@"Helvetica-Bold" size:14] constrainedToSize:constraint1 lineBreakMode:UILineBreakModeWordWrap]; 
     UILabel *name1 = (UILabel *)[cell viewWithTag:111]; 
     name1.frame = CGRectMake(105,25, 175, size1.height); 
     [name1 setNumberOfLines:size1.height/16]; 
     name1.text = [d objectForKey:@"Name"]; 
     [name1 setTextColor:UIColorFromRGB(COLOR_BLUE)]; 


     NSString *codetype2 = [d objectForKey:@"Type"]; 
     CGSize constraint2 = CGSizeMake(175, 2000.0f); 
     CGSize size2 = [codetype2 sizeWithFont: [UIFont fontWithName:@"Verdana" size:12] constrainedToSize:constraint2 lineBreakMode:UILineBreakModeWordWrap]; 
     UILabel *codetype1 = (UILabel *)[cell viewWithTag:112]; 
     codetype1.frame = CGRectMake(105,name1.frame.size.height+25, 175, size2.height); 
     [codetype1 setNumberOfLines:size2.height/16]; 
     codetype1.text=[d objectForKey:@"CodeType"]; 

     NSString *line2 = [d objectForKey:@"Line1"]; 
     NSString *line4 = [d objectForKey:@"Line2"]; 

     if([line2 isEqualToString:@""]) 
     { 
      if([line4 isEqualToString:@""]) 
      { 
       NSString *city2 = [d objectForKey:@"City"]; 
       CGSize constraint4 = CGSizeMake(175, 2000.0f); 
       CGSize size4 = [city2 sizeWithFont: [UIFont fontWithName:@"Verdana" size:12] constrainedToSize:constraint4 lineBreakMode:UILineBreakModeWordWrap]; 
       city = (UILabel *)[cell viewWithTag:115]; 
       city.frame = CGRectMake(105,codetype1.frame.size.height+codetype1.frame.origin.y, 175, size4.height); 
       [city setNumberOfLines:0]; 
       city.text = [d objectForKey:@"City"]; 
      } 
      else 
      { 
       NSString *line4 = [d objectForKey:@"Line2"]; 

       CGSize constraint4 = CGSizeMake(175, 2000.0f); 
       CGSize size4 = [line4 sizeWithFont: [UIFont fontWithName:@"Verdana" size:12] constrainedToSize:constraint4 lineBreakMode:UILineBreakModeWordWrap]; 
       line3 = (UILabel *)[cell viewWithTag:114]; 
       line3.frame = CGRectMake(105,codetype1.frame.size.height+codetype1.frame.origin.y, 175, size4.height); 
       [line3 setNumberOfLines:0]; 
       line3.text = [d objectForKey:@"Line2"]; 


       NSString *city2 = [d objectForKey:@"City"]; 
       CGSize constraint5 = CGSizeMake(175, 2000.0f); 
       CGSize size5 = [city2 sizeWithFont: [UIFont fontWithName:@"Verdana" size:12] constrainedToSize:constraint5 lineBreakMode:UILineBreakModeWordWrap]; 
       city = (UILabel *)[cell viewWithTag:115]; 
       city.frame = CGRectMake(105,line3.frame.size.height+line3.frame.origin.y, 175, size5.height); 
       [city setNumberOfLines:0]; 
       city.text = [d objectForKey:@"City"]; 





      } 


     } 
     else 
     { 
      NSString *line2 = [d objectForKey:@"Line1"]; 
      CGSize constraint3 = CGSizeMake(175, 2000.0f); 
      CGSize size3 = [line2 sizeWithFont: [UIFont fontWithName:@"Verdana" size:12] constrainedToSize:constraint3 lineBreakMode:UILineBreakModeWordWrap]; 
      line1 = (UILabel *)[cell viewWithTag:113]; 
      line1.frame = CGRectMake(105,codetype1.frame.size.height+codetype1.frame.origin.y, 175, size3.height); 
      [line1 setNumberOfLines:0]; 
      line1.text = [d objectForKey:@"Line1"]; 

      if([line4 isEqualToString:@""]) 
      { 
       NSString *city2 = [d objectForKey:@"City"]; 
       CGSize constraint5 = CGSizeMake(175, 2000.0f); 
       CGSize size5 = [city2 sizeWithFont: [UIFont fontWithName:@"Verdana" size:12] constrainedToSize:constraint5 lineBreakMode:UILineBreakModeWordWrap]; 
       city = (UILabel *)[cell viewWithTag:115]; 
       city.frame = CGRectMake(105,line1.frame.size.height+line1.frame.origin.y, 175, size5.height); 
       [city setNumberOfLines:0]; 
       city.text = [d objectForKey:@"City"]; 
      } 
      else 
      { 
       NSString *line4 = [d objectForKey:@"Line2"]; 

       CGSize constraint4 = CGSizeMake(175, 2000.0f); 
       CGSize size4 = [line4 sizeWithFont: [UIFont fontWithName:@"Verdana" size:12] constrainedToSize:constraint4 lineBreakMode:UILineBreakModeWordWrap]; 
       line3 = (UILabel *)[cell viewWithTag:114]; 
       line3.frame = CGRectMake(105,line1.frame.size.height+line1.frame.origin.y, 175, size4.height-3); 
       [line3 setNumberOfLines:0]; 
       line3.text = [d objectForKey:@"Line2"]; 



       NSString *city2 = [d objectForKey:@"City"]; 
       CGSize constraint5 = CGSizeMake(175, 2000.0f); 
       CGSize size5 = [city2 sizeWithFont: [UIFont fontWithName:@"Verdana" size:12] constrainedToSize:constraint5 lineBreakMode:UILineBreakModeWordWrap]; 
       city = (UILabel *)[cell viewWithTag:115]; 
       city.frame = CGRectMake(105,line3.frame.size.height+line3.frame.origin.y+4, 175, size5.height); 
       [city setNumberOfLines:0]; 
       city.text = [d objectForKey:@"City"]; 



      } 



     } 



     UILabel *state=[[UILabel alloc]initWithFrame:CGRectMake(105,city.frame.size.height+city.frame.origin.y+5,320,10)]; 
     state.font=[UIFont fontWithName:@"Helvetica" size:12]; 
     [state setTextAlignment:UITextAlignmentLeft]; 
     [state setText:[d valueForKey:@"State"]]; 
     state.tag=116; 
     [cell addSubview:state]; 
     [state release]; 

} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 

     return 1; 


} 
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
UIView *sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, tableView.sectionHeaderHeight)]; 
    sectionHeaderView.backgroundColor = [UIColor colorWithWhite:0 alpha:0]; 
    sectionHeaderView.userInteractionEnabled = YES; 
    sectionHeaderView.tag = section; 


    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(12,2, tableView.bounds.size.width-10, 18)]; 
    headerLabel.backgroundColor = [UIColor whiteColor]; 
    [headerLabel setTextAlignment:UITextAlignmentCenter]; 
    headerLabel.textColor = [UIColor grayColor]; 
    headerLabel.shadowColor = [UIColor darkGrayColor]; 
    headerLabel.shadowOffset = CGSizeMake(0, 1); 
    headerLabel.font = [UIFont boldSystemFontOfSize:18]; 
    headerLabel.text = @"Details"; 


    [sectionHeaderView addSubview:headerLabel]; 

    return sectionHeaderView; 
    } 

在這裏,我給了它是205。但是當tableViewCell增加的文本,然後我無法看到的最後tableViewCell標籤的最後一行..

我怎樣才能讓tableViewCellauto-resize它的高度?

+0

發表您的表格視圖委託方法的代碼太 –

+0

我已編輯它。你可以看看,讓我知道在哪裏修改? – Honey

回答

1

您可以在– tableView:heightForRowAtIndexPath:中指定單元的高度。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CGFloat height=60; //set default row height 
    if(check_for_the_rows_which_need_increased_height) { 
     height+=50; //Add appropriate increase 
    } 
    return height; 
} 

你需要調用[yourTableView reloadData][yourTableView reloadRowsAtIndexPaths:indexPath withRowAnimation:UITableViewRowAnimationAutomatic]用於上述方法再次調用。

+0

我可以知道這是什麼嗎? index_of_row_which_needs_increased_height – Honey

+0

我已更新答案。將check_for_the_rows_which_need_increased_height替換爲你的條件來檢查哪些行應該增加高度 – Atif

+0

但是我不知道哪一行need_increased_height bcoz所有的行以及它裏面的數據在運行時都會改變..所以我應該給那裏的check_for_the_rows_which_need_increased_height? – Honey

1

您可以直接檢查你textHeight不同,然後設置你的行高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    NSString *Yourtext = [yourTextAry objectAtIndex:[indexPath row]]; 

    CGSize constraint = CGSizeMake(yourLabelWidth , 20000.0f); 

    CGSize size = [Yourtext sizeWithFont: [UIFont fontWithName:@"Verdana" size:13] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

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

    return height; 
} 

,如果文本爲空,然後從陣列中刪除它親愛的或只是通過高度爲零.. :)

+0

ki會試一試讓你知道.. – Honey

+0

是的..爲什麼不是.. :) – Rajneesh071

+0

... ???解決了?。 – Rajneesh071

相關問題