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 9311415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:21:32+00:00 2026-06-19T01:21:32+00:00

So, it seems that heightForRowAtIndexPath is called before cellForRowAtIndexPath . I’m currently using cellForRowAtIndexPath

  • 0

So, it seems that heightForRowAtIndexPath is called before cellForRowAtIndexPath. I’m currently using cellForRowAtIndexPath to calculate the height of each cell (they’re variable heights, using a UILabel with varying amounts of text in it.

I’m puzzled as to exactly how I can set the cell’s height correctly if the method that sets the height is called before the method that calculates it.

I’ve created a category of NSString, and another of UILabel, which working together make the UILabel the right size. The issue is that the UITableViewCell instances which contain those labels aren’t being resized, so the labels hang over the end of each label, overlapping the ones beneath.

I know that what I need to do is make the height of the cell equal the height of the label, plus some extra margin space, but I’m puzzled as to how I can set the height before the label height has been calculated. Can I set the height someone without using heightForRowAtIndexPath? Or if not, could I calculate the height of the cell elsewhere? I’m currently using indexPath.row to make that happen, so doing it beforehand would be tricky. Here’s the code my cellForRowAtIndexPath:

cellForRowAtIndexPath

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
    FQCustomCell *_customCell = [tableView dequeueReusableCellWithIdentifier: @"CustomCell"];
    if (_customCell == nil) {
        _customCell = [[FQCustomCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"CustomCell"];
    }

    _customCell.myLabel.text = [[_loadedNames objectAtIndex: indexPath.row] name];
    _customCell.myLabel.font = [UIFont fontWithName: @"FontA" size: 15.0f];

    UIView *backView = [[UIView alloc] initWithFrame: CGRectZero];
    backView.backgroundColor = [UIColor clearColor];
    _customCell.backgroundView = backView;

    _customCell.myLabel.frame = CGRectMake(5, 5, 265, 65);

    [_customCell.myLabel sizeToFitMultipleLines];
    return _customCell;
}
  • 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-19T01:21:33+00:00Added an answer on June 19, 2026 at 1:21 am

    You need to calculate the height of the cell in heightForRowAtIndexPath, something like

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        NSString *theText=[[_loadedNames objectAtIndex: indexPath.row] name];
        CGSize labelSize = [theText sizeWithFont:[UIFont fontWithName: @"FontA" size: 15.0f] constrainedToSize:kLabelFrameMaxSize];
        return kHeightWithoutLabel+labelSize.height;
    }
    

    You can set some constraints on the label size by setting kLabelFrameMaxSize

    #define kLabelFrameMaxSize CGSizeMake(265.0, 200.0)
    

    Then you return the height by adding a constant height with the variable label height.

    Also, to be consistent, you should use the same methodology to set the frame in cellForRowAtIndexPath instead of using sizeToFitMultipleLines

    - (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
    {
        FQCustomCell *_customCell = [tableView dequeueReusableCellWithIdentifier: @"CustomCell"];
        if (_customCell == nil) {
            _customCell = [[FQCustomCell alloc] initWithStyle: UITableViewCellStyleValue1 reuseIdentifier: @"CustomCell"];
        }
    
        _customCell.myLabel.text = [[_loadedNames objectAtIndex: indexPath.row] name];
        _customCell.myLabel.font = [UIFont fontWithName: @"FontA" size: 15.0f];
    
        UIView *backView = [[UIView alloc] initWithFrame: CGRectZero];
        backView.backgroundColor = [UIColor clearColor];
        _customCell.backgroundView = backView;
    
        CGSize labelSize = [_customCell.myLabel.text sizeWithFont:_customCell.myLabel.font constrainedToSize:kLabelFrameMaxSize];
        _customCell.myLabel.frame = CGRectMake(5, 5, labelSize.width, labelSize.height);
    
        return _customCell;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems that the substitute(f,t) function in Z3Py performs simplification on f first before
I'm using the following code to size a UITableViewCellStyleSubtitle cell to fit text that
Seems that Ruby IO#getc wait until receiving a \n before returning the chars. If
It seems that altough I have been using NHibernate for a while now, I
Seems that I can't store the linguettaCorrente variable into the hover handler : $('.navigatore_blocco').hover(
Seems that web.xml for a servlet has an element called <enabled>false</enabled> that can be
Seems that This will be an easy question for you but this problem is
Seems that even after unchecking the option in the PyDev/Debug preferenecs pane to launch
It seems that we can show layers and even use a different zPosition for
It seems that runClasses() doesn't terminate the code being tested even after the test

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.