如何在UITableView静态单元格中dynamic更改单元格高度

我使用UITableViewController而不是detailView来显示一个实体的细节。 我在我的viewDidLoad方法中填充了PFQuery的一行数据。 我有一个单元格,我正在使用标签,我想根据NSString大小更改大小。 我检查了很多答案,但都与dynamic的indexPath有关,我有静态单元格。 My label显示完整的string,但我的细胞是固定的。 我怎样才能改变细胞的高度? 请帮我解决这个问题。

这是我的代码:

 - (void)viewDidLoad { [super viewDidLoad]; PFQuery *query = [PFQuery queryWithClassName:@"Entities"]; [query whereKey:@"name" equalTo:entityName]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (!error) { PFFile *thumbnail = [objects[0] objectForKey:@"image"]; detailsImageView.image = [UIImage imageNamed:@"loader.gif"]; detailsImageView.file = thumbnail; [detailsImageView loadInBackground]; detailLabel.numberOfLines = 0; // this label need dynamic height and its cell detailLabel.text = [objects[0] objectForKey:@"descriptionLarge"]; [detailLabel sizeToFit]; } else { NSString *errorString = [[error userInfo] objectForKey:@"error"]; NSLog(@"Error: %@", errorString); } }]; } 

我想通过select单个单元格,但它给了我UITableView错误,因为我没有定义tableView,我怎么能定义或如果你有任何其他好的解决scheme,请让我知道。

 static NSString *simpleTableIdentifier = @"DetailsDesecriptionCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

我也开放批评质量编码,所以你的反馈对我很有价值。

如果您使用自动布局。 将顶部,左侧和右侧限制添加到您的标签。

然后在heightForRowAtIndexPath创build你的单元格

 static NSString *simpleTableIdentifier = @"DetailsDesecriptionCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

设置值以标记并在设置值后计算标签的大小

  [cell layoutIfNeeded]; return cell.label.frame.origin.y+cell.label.frame.size.height; 

这会给你标签的确切高度和你的细胞高度是一样的

 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *simpleTableIdentifier = @"DetailsDesecriptionCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; cell.titleLabel.text=@"YOUR TEXT"; [cell layoutIfNeeded]; return cell.titleLabel.frame.origin.y+cell.titleLabel.frame.size.height; } 

如果是IOS 8或9,有一个简单的修复。

  1. 设置UILabel的约束。 (顶部,左侧,底部,右侧)
  2. 将UILabel的行设置为0
  3. 在ViewController的viewDidLoad方法中添加以下代码:
 tableView.estimatedRowHeight = 68.0 tableView.rowHeight = UITableViewAutomaticDimension 
  1. 覆盖function:
 override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return UITableViewAutomaticDimension; } 

我一直在努力做到这一点从多天,但不能。 我尝试了Fahim的回答,并得到了线索,下面我从其他不同的答案,并已能够完成它。 我在代码中添加以下函数来获得我的标签的确切大小

 -(CGFloat)getLabelHeightForText:(NSString *)text andWidth:(CGFloat)labelWidth { CGSize maximumSize = CGSizeMake(labelWidth, 10000); NSLog(@"crossing getLabelheightForText"); //provide appropriate font and font size CGSize labelHeighSize = [text sizeWithFont: [UIFont fontWithName:@"Trebuchet MS" size:17.0f] constrainedToSize:maximumSize lineBreakMode:NSLineBreakByTruncatingTail]; return labelHeighSize.height; } 

比我添加indexPath与所有单元格大小要返回,这里只显示一个单元格大小。

  -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { switch (indexPath.section) { case 0: { if (indexPath.row == 0) // category name size return 44.0; if (indexPath.row == 1) // image size return 204; } default: return 44.0 } } 

但在此之后,我得到了完美的标签大小,但仍然有更多的工作,因为我正在使用PFQuery和tableView indexPath首先运行,这就是为什么它不能更新我的表,我仍然无法调整单元格大小。 比这里是重新加载我的tableView和解决我的问题的魔术线

 [self.tableView reloadData]; 

虽然问题很简单,但是有很多东西,所以我把答复当作帮助别人。

以下是我的工作。

 // this will set height of the row -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ String *yourLongString = "Long text here"; UILabel *mLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,320,30)]; your size will change here mLabel.hidden = YES; mLabel.text = yourLongString; [mLabel sizeToFit]; ^^^^^^^^ this is very important return mLabel.frame.size.height; } 

现在在cellForRowAtIndexPath ,根据单元格的高度调整实际标签的高度。

如果你不清楚,请告诉我。