ios – 如何在UITableView静态单元格中动态更改单元格高度

前端之家收集整理的这篇文章主要介绍了ios – 如何在UITableView静态单元格中动态更改单元格高度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用UITableViewController而不是detailView来显示一个实体细节.我在viewDidLoad方法中填充了PFQuery中的一行数据.我有一个单元格,我使用标签,我想根据NSString大小更改大小.我检查了很多答案,但都与indexPath相关,这是动态的,我有静态单元格.我的标签显示完整的字符串,但我的单元格已修复.如何更改单元格的高度?请帮我纠正这个问题.

这是我的代码

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. PFQuery *query = [PFQuery queryWithClassName:@"Entities"];
  5. [query whereKey:@"name" equalTo:entityName];
  6. [query findObjectsInBackgroundWithBlock:^(NSArray *objects,NSError *error) {
  7. if (!error) {
  8. PFFile *thumbnail = [objects[0] objectForKey:@"image"];
  9. detailsImageView.image = [UIImage imageNamed:@"loader.gif"];
  10. detailsImageView.file = thumbnail;
  11. [detailsImageView loadInBackground];
  12. detailLabel.numberOfLines = 0; // this label need dynamic height and its cell
  13. detailLabel.text = [objects[0] objectForKey:@"descriptionLarge"];
  14. [detailLabel sizeToFit];
  15. } else {
  16. NSString *errorString = [[error userInfo] objectForKey:@"error"];
  17. NSLog(@"Error: %@",errorString);
  18. }
  19. }];
  20. }

我试图通过选择单个单元格来执行以下操作,但它给了我UITableView错误,因为我没有定义tableView,如何定义或者如果您有任何其他好的解决方案请告诉我.

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

我也对批评编码质量持开放态度,因此您的反馈对我来说很有价值.

解决方法

如果您使用autolayout.将顶部,左侧和右侧约束添加标签中.

然后在heightForRowAtIndexPath中创建您的单元格

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

在设置值后,将值设置为标签并计算标签的大小

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

这将为您提供标签的确切高度,您的单元格高度将相同

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

猜你在找的iOS相关文章