I am using UITableViewController
instead detailView to show one entity details. I populated one row of data from PFQuery in my viewDidLoad
method. I have one cell where I am using label, I want to change the size as per NSString
size. I checked a lot of answers but all are related to indexPath
which is for dynamic and I have static cells. My label
showing complete string, but my cell is fixed. How can I change the height of the cell? Please help me to correct this problem.
Here is my code:
- (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);
}
}];
}
I am trying to do the following by selecting individual cells but it gave me UITableView error as I have not define tableView, how can I define or if you have any other good solution please let me know.
static NSString *simpleTableIdentifier = @"DetailsDesecriptionCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
I am also open on criticism of about coding quality so your feedback is valuable for me.
If you are using autolayout. Add top, left and right constrain in to your label.
Then in heightForRowAtIndexPath
create your cell
static NSString *simpleTableIdentifier = @"DetailsDesecriptionCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
Set values to label and calculate size of the label after setting the value
[cell layoutIfNeeded];
return cell.label.frame.origin.y+cell.label.frame.size.height;
This will give you the exact hight of the label and your cell height will be same
-(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;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments