我使用UITableViewController而不是detailView来显示一个实体细节.我在viewDidLoad方法中填充了PFQuery中的一行数据.我有一个单元格,我使用标签,我想根据NSString大小更改大小.我检查了很多答案,但都与indexPath相关,这是动态的,我有静态单元格.我的标签显示完整的字符串,但我的单元格已修复.如何更改单元格的高度?请帮我纠正这个问题.
这是我的代码:
- - (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);
- }
- }];
- }
我试图通过选择单个单元格来执行以下操作,但它给了我UITableView错误,因为我没有定义tableView,如何定义或者如果您有任何其他好的解决方案请告诉我.
- static NSString *simpleTableIdentifier = @"DetailsDesecriptionCell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
我也对批评编码质量持开放态度,因此您的反馈对我来说很有价值.
解决方法
如果您使用autolayout.将顶部,左侧和右侧约束添加到标签中.
然后在heightForRowAtIndexPath中创建您的单元格
- 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;
- }