Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8017261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:47:42+00:00 2026-06-04T20:47:42+00:00

I am implementing a UITableView in which the table cells contain several controls, such

  • 0

I am implementing a UITableView in which the table cells contain several controls, such as a labels and buttons. One of the labels toward the top of the cell can have varying amounts of text, thus the entire size of each cell must vary as well. So basically, it’s the dynamic table cell problem that you see all over the place.

The de facto solution that I seem to find is this one
, which I am using, but I don’t care much for it because it comes with maintenance problems. Since I have so many other things in the cell, I have to have a lot of hardcoded heights to represent all the other controls that must be accounted for when determining the overall size of the cell and when laying everything out based on the size of the dynamic content.

Here is some code:

- (CGFloat) tableView:(UITableView*)tableView
            heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    SomeDataItem* item = [self.myData objectAtIndex:indexPath.row];
    CGSize constraintSize = CGSizeMake(275, MAXFLOAT);
    CGSize labelSize = [item.message sizeWithFont:[UIFont systemFontOfSize:13]
                                constrainedToSize:constraintSize 
                                    lineBreakMode:UILineBreakModeWordWrap];

    return HEADER_HEIGHT + OUTER_MARGIN + labelSize.height +
           INNER_MARGIN + INNER_MARGIN + OUTER_MARGIN +
           BUTTON_HEIGHT + OUTER_MARGIN;
}

- (UITableViewCell*)tableView:(UITableView*)tableView
                    cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    ...
    NSString* message = //some dynamic text
    CGSize constraintSize = CGSizeMake(275, MAXFLOAT);
    CGSize labelSize = [message sizeWithFont:[UIFont systemFontOfSize:13]
                           constrainedToSize:constraintSize
                               lineBreakMode:UILineBreakModeWordWrap];

    //resize label according to amount of text
    cell.messageLabel.frame = CGRectMake(cell.messageLabel.frame.origin.x,
                                         cell.messageLabel.frame.origin.y,
                                         cell.messageLabel.frame.size.width, 
                                         labelSize.height);

    //now move controls below the dynamic label down
    y = HEADER_HEIGHT + OUTER_MARGIN;
    [self setView:cell.messageTextBackground yCoord:y];

    y += INNER_MARGIN;
    [self setView:cell.messageLabel yCoord:y];

    y += labelSize.height;
    [self setView:cell.anotherLabel yCoord:y];

    y += OUTER_MARGIN;
    [self setView:cell.someButton yCoord:y];
}

- (void) setView:(UIView*)view yCoord:(NSInteger)y
{
    int x = view.frame.origin.x;
    int w = view.frame.size.width;
    int h = view.frame.size.height;
    view.frame = CGRectMake(x, y, w, h);
}

That’s just kind of an approximation of the code to give you an idea of what I’m doing. As you can see, it’s kind of a mess. If I have to keep #define values for all the other controls beside the dynamic label. If I want to change the size of any of these controls I have to do it in the XIB file and then also remember to update the #define values. Even worse, if I want to introduce some new controls, I have to modify both the of these methods and make sure everything is still getting calculated and offset properly.

This seems pretty absurd to me, but I haven’t found any other way to doing it. If only you could get access to the table cells within tableView:heightForRowAtIndexPath, then you could just calculate everything based on the actual controls, instead of having to hard code everything. However, from what I’ve read and experienced tableView:heightForRowAtIndexPath gets called before the cell even exists so you can’t do that.

Am I off on how I am handling this or is this the only way to go?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:47:44+00:00Added an answer on June 4, 2026 at 8:47 pm

    I think you are doing the right thing. At least that is how I have done it for a while. Maintenance should be OK, you just have to do the things you mentioned.

    You could perhaps cut down on your #define statements by using a XIB file for the cell, loading the xib file in viewDidLoad and extracting the sizes of the various elements there. You would then only have to change them in the XIB file in the future.

    Of course a wrapper view containing the non-changing elements could be an additional help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am implementing a UItableView with custom cells. My custom cell has a UIView
Quick question here. I'm implementing a UITableView with cells, some of the cells contain
When implementing the Strategy Pattern, where does one put the code that determines which
I am implementing a UITableView with two images in a cell. Both images will
I am trying to create a table view which uses two types of cells
I'm implementing a UITableView that holds a few custom UITableViewCells, each of which contains
Implementing a custom membership provider, there are certain properties such as MinRequiredPasswordLength that only
When implementing the ISerializable interface in C#, we provide a constructor which takes a
I have a question regarding uitable view. I am implementing an app which is
Implementing a low pass FIR filter, when should one use FFT and IFFT instead

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.