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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:13:35+00:00 2026-06-09T08:13:35+00:00

With the help of the good people of StackOverflow, I have these two methods:

  • 0

With the help of the good people of StackOverflow, I have these two methods:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault)
            reuseIdentifier:@"business"];

    NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")];

    NSLog(businessPrivacy);
    // FIND IF THE BUSINESS PLAN IS PRIVATE OR NOT.

        CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f); 

        CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, MAX(size.height, 44.0f) + 20.0f)]; 
        label.numberOfLines = 0; 
        label.lineBreakMode = UILineBreakModeWordWrap; 
        label.text = comment;  

        [cell.contentView addSubview:label];        

    return cell;
}

//Cell size for word wrapping.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{    
    NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")];

    CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f); 

    CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 

    CGFloat height = MAX(size.height, 44.0f);

    return height + (10 * 2);
}

The effect that they have is reducing font size from default font size, and dynamically resizing to ALMOST fit the entire text of a comment no matter how long that text might be.

What I am confused with is that I have some of the same code in both methods, and I dont know how it really should look like. I do know both methods are getting called, but not sure what should be where so they would work properly.

Thanks in advance for advice.

  • 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-09T08:13:36+00:00Added an answer on June 9, 2026 at 8:13 am

    It looks like you need them in both. I have rewritten your code with comments so that you can make sense of it:

    //This function is used to create the cell and the content of the cell.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {   
        //Here you are allocating a new cell with a reuse identifier.  This is only needed if
        //you plan on reusing the cells and using [tableView dequeueReusableCellWithIdentifier:cellIdentifier]
        //Reusable cells will save you some memory and make your tableview work more smoothly,
        //however here you are not selecting from the reusable pool, and are instead making a new cell each time
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault)
                reuseIdentifier:@"business"];
    
        //This is pulling the text for the comment, it is obviously necessary
        NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")];
    
        NSLog(businessPrivacy);
        // FIND IF THE BUSINESS PLAN IS PRIVATE OR NOT.
    
        // This is creating a constraint size for the label you are making
        CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f); 
    
        // This is determining the size that you will need for the label based on the comment
        // length and the contraint size
        CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 
    
        //  Here you are creating your label and initializing it with the frame that you have
        //  determined by your size calculations above
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, MAX(size.height, 44.0f) + 20.0f)];
    
        // setting up the label properties
        label.numberOfLines = 0;
        label.lineBreakMode = UILineBreakModeWordWrap; 
        label.text = comment;  
    
        // adding the label view to the cell that you have created
        [cell.contentView addSubview:label];        
    
        // return the cell for the table view
        return cell;
    }
    
    //Cell size for word wrapping.
    //This method will determine how tall each row needs to be.
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
    {
        //Here you are getting the comment again.  This is necessary to determine how tall you need
        //the cell to be    
        NSString *comment = [[items_array objectAtIndex:[indexPath row]] objectForKey:(@"comment")];
    
        // Again you are getting the constraints because you are going to us this size
        // to constrain the height of the cell just like you determined the size for the label.
        CGSize constraint = CGSizeMake(320 - (10 * 2), 20000.0f); 
    
        // Again, determining the size that we will need for the label, because it will drive
        // the height of the cell
        CGSize size = [comment sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 
    
        //  Finally, we can determine the height for the cell!
        CGFloat height = MAX(size.height, 44.0f);
    
        //  return the height, which is the height of the label plus some extra space to make
        //  it look good.
        return height + (10 * 2);
    }
    

    So basically, the cellForRow... function creates the content of the cell and the heightForRow... function determines the height. You need all of that information in both of them because the cell function only creates the content of the cell while the height function creates the height of the row and the comment, constraint, and size are all important in determine the label size for the cell content and the height of the cell.

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

Sidebar

Related Questions

Hopefully the good people at Stackoverflow can help me today - I basically drank
Greetings good people - I need some help. I have until now buildt web-pages
Hi there good people of StackOverflow, I have a problem and I was hoping
Good people of StackOverflow, please help. I've set up an ejabberd server on my
I have heard a lot of people saying that to be a good programmer
Good afternoon people's I have built a jquery plugin that I use on my
Good people - Need a little help. I am making a site that will
Good people - I need some help to find a way to create an
Hello good people of Stack Overflow, I have come with yet another question for
Greetings good people of the internet:) I have a string value 1,5 which after

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.