Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change cell height dynamically in UITableView static cell

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.

like image 273
Kamal Panhwar Avatar asked Aug 21 '14 07:08

Kamal Panhwar


People also ask

How do you make a table dynamic cell height?

To get dynamic cell heights working properly, you need to create a custom table view cell and set it up with the right Auto Layout constraints. In the project navigator, select the Views group and press Command-N to create a new file in this group.

How do I change the dynamic height of a table in Swift?

Set your view controller constraints in the Storyboard and then create an outlet to the UITableView height constraint in your UIViewController subclass (or the constraint that controls the height of the UITableView like the distance to the bottom of the screen).


1 Answers

If it is IOS 8 OR 9, there is a simple fix.

  1. Set up the constrains for the UILabel. (top, left, bottom, right)
  2. Set lines of the UILabel to 0
  3. Add the following code in the viewDidLoad method of the ViewController:
tableView.estimatedRowHeight = 68.0
tableView.rowHeight = UITableViewAutomaticDimension
  1. override the function:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}
like image 122
L.L. Avatar answered Oct 23 '22 18:10

L.L.