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

  • Home
  • SEARCH
  • 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 5959529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:41:52+00:00 2026-05-22T18:41:52+00:00

In a UITableView, I add content to my cells via UILabels. To define the

  • 0

In a UITableView, I add content to my cells via UILabels.

To define the optimal size (as large as allowed by the cell’s width) I noticed that only tableView.contentSize.width was reliable, because cell.contentView.bounds gives always a portrait width, even in landscape

This works very well in couple with autoresizingMask: when I switch from Portrait to Landscape and again to Portrait.

Problems come when I load my View directly in Landscape. The width of my UILabels is larger than the screen, even if a breakpoint shows me a correct width for tableView.contentSize.width

Switching between landscape and portait changes nothing, the width is still larger than the screen.

If I don’t use autoresizingMask, the width is correct, if I use it, even with a short text it goes out of the screen (but I notice it only thanks to a test background color or with using very large NSString).

In brief:

  • portrait > landscape > portrait > landscape… is fine (resize perfectly)
  • landscape > portrait > landscape > portrait… bugs (width outrange)

My code simplified:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    //[...]
    UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];
    CGRect frame = CGRectMake(cell.contentView.bounds.origin.x + 20,
                                  cell.contentView.bounds.origin.y + 4,
                                  tableView.contentSize.width - 50,
                                  font.lineHeight);

      //at this point a breakpoint shows that frame is whatever I asked (ex: 200 for test)
      //but still the labels is wider than the screen
    UILabel *result = [[[UILabel alloc] initWithFrame:frame] autorelease];
    [result setText:@"bouh!"];
    result.lineBreakMode = UILineBreakModeWordWrap;
    result.numberOfLines = 1;
    result.font = font;

      //if I comment this line, the width is always what I want
    result.autoresizingMask = UIViewAutoresizingFlexibleWidth;

      //test to see the real size
    result.backgroundColor = [UIColor orangeColor];

    [cell.contentView addSubview:result];
    //[...]
    return cell;
}

I’m asking your help here in order to see if there’s a better way to do what I wanted? if not, what am I doing wrong?

  • 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-05-22T18:41:53+00:00Added an answer on May 22, 2026 at 6:41 pm

    I could not find a clean easy universal solution (adaptable to every screen).

    But here is what I did to make it work:

        //in portrait cell width works fine with autoResize
        //in landscape tableView width works fine with autoResize
    CGFloat cellWidth;
    
    if ( [UIDevice currentDevice].orientation != UIDeviceOrientationLandscapeLeft
        && [UIDevice currentDevice].orientation != UIDeviceOrientationLandscapeRight)
      {
        cellWidth = tableView.contentSize.width;
      }
    else
      {
        cellWidth = cell.contentView.bounds.size.width;
      }
    

    And so, later in the code I use it that way:

    CGRect frame = CGRectMake(cell.contentView.bounds.origin.x + 20,
                                      cell.contentView.bounds.origin.y + 4,
                                      cellWidth - 50,
                                      font.lineHeight);
    UILabel *result = [[[UILabel alloc] initWithFrame:frame] autorelease];
    

    But something was weird: I needed to use always tableView.contentSize.width for the frame of my Activity Indicators because cell.contentView.bounds.size.width was only 320 even in Landscape

    CGRect activityIndicatorRect =
    CGRectMake(
               cell.contentView.bounds.origin.x + tableView.contentSize.width - 60 ,
               cell.contentView.bounds.origin.y + 17,
               30, 30);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering if it is possible to add cell content to a uitableview based
I've a UITableView which when a cell has the same content that other, this
I have a UITableView that's experiencing performance problems on older phones. The table cells
For some strange reason, UITableView resizes my cells to 320 width no matter how
I have a UITableView with about 20 cells, in each cell there are three
I need to add some blank space to the top of my UITableView that
I've got a UIView that I'm adding to a cell's content view in a
I'm making an app that as a UITableView that gets content from the web,
I am trying to add a UITableView object that covers up the whole screen,
Why can't I add a subview to table view cell's content view? - (UITableViewCell

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.