How to change cell height dynamically in UITableView static cell

KamalPanhwar

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.

Jay Gajjar

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

UITableView: How to change cell height dynamically when a button is clicked in it?

From Dev

UITableView: How to change cell height dynamically when a button is clicked in it? Swift

From Dev

Dynamically change TableView Cell height

From Dev

Dynamically change cell height programmatically

From Dev

Get UITableView cell height

From Dev

Uitableview cell dynamic height

From Dev

How to make fixed height cell on uitableview?

From Dev

How dynamically to change the formula of a cell

From Dev

UITableView inside static cell

From Dev

Static cell at the bottom of a UITableView

From Dev

How to dynamically set a cell position in a UITableView

From Dev

Swift: How to use static cell in dynamic UITableView

From Dev

How to add a Cell into a static section UITableView?

From Dev

How to add a Cell into a static section UITableView?

From Dev

Swift: How to use static cell in dynamic UITableView

From Dev

Unnatural jerk with UITableView whe cell height is changed dynamically

From Dev

Static UITableView, make a single cell with dynamic height in Swift

From Dev

How to set automatic height of static cell in UITableViewController?

From Dev

UItableview each cell label want to hide and height should be change

From Dev

How to change colour of selected cell in UITableView?

From Dev

How to change colour of selected cell in UITableView?

From Dev

Change cell height but not elements in it

From Dev

how to change the height of a single cell when clicked?

From Dev

Dynamic UITableView inside static cell

From Dev

Dynamic UITableView inside static cell

From Dev

PHPExcel How to apply styles and set cell width and cell height to cell generated dynamically

From Dev

jQuery add dynamically cell to a static cell into table

From Dev

Calculating collectionView cell height dynamically

From Dev

How to programmatically increase UITableView cell's height in iPhone?

Related Related

  1. 1

    UITableView: How to change cell height dynamically when a button is clicked in it?

  2. 2

    UITableView: How to change cell height dynamically when a button is clicked in it? Swift

  3. 3

    Dynamically change TableView Cell height

  4. 4

    Dynamically change cell height programmatically

  5. 5

    Get UITableView cell height

  6. 6

    Uitableview cell dynamic height

  7. 7

    How to make fixed height cell on uitableview?

  8. 8

    How dynamically to change the formula of a cell

  9. 9

    UITableView inside static cell

  10. 10

    Static cell at the bottom of a UITableView

  11. 11

    How to dynamically set a cell position in a UITableView

  12. 12

    Swift: How to use static cell in dynamic UITableView

  13. 13

    How to add a Cell into a static section UITableView?

  14. 14

    How to add a Cell into a static section UITableView?

  15. 15

    Swift: How to use static cell in dynamic UITableView

  16. 16

    Unnatural jerk with UITableView whe cell height is changed dynamically

  17. 17

    Static UITableView, make a single cell with dynamic height in Swift

  18. 18

    How to set automatic height of static cell in UITableViewController?

  19. 19

    UItableview each cell label want to hide and height should be change

  20. 20

    How to change colour of selected cell in UITableView?

  21. 21

    How to change colour of selected cell in UITableView?

  22. 22

    Change cell height but not elements in it

  23. 23

    how to change the height of a single cell when clicked?

  24. 24

    Dynamic UITableView inside static cell

  25. 25

    Dynamic UITableView inside static cell

  26. 26

    PHPExcel How to apply styles and set cell width and cell height to cell generated dynamically

  27. 27

    jQuery add dynamically cell to a static cell into table

  28. 28

    Calculating collectionView cell height dynamically

  29. 29

    How to programmatically increase UITableView cell's height in iPhone?

HotTag

Archive