0

I am trying to set the label width dynamically based on the content of the other label on the same line.

I am implementing the logic inside the "cellForRowAtIndexPath"

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"rowNumber:%d", indexPath.row);
    EntityTableViewCell *cell = nil;
    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.companyNameLabel.text = [group1 objectAtIndex:indexPath.row];
    cell.companyCROfficeAddrLabel.text = [group2 objectAtIndex:indexPath.row];
    cell.companyBusinessAddrLabel.text = [group3 objectAtIndex:indexPath.row];

    cell.timestamp.text = [Utils getDateTimeString:[group4 objectAtIndex:indexPath.row]];

    [cell.companyLogoImageView setImageWithURL:[NSURL URLWithString:[myArray objectAtIndex:indexPath.row] ]
                              placeholderImage:[UIImage imageNamed: @"profile-image-placeholder"]
                                       options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];

    //logics to dynamically change the label width to maximum utilize the realstate



    CGFloat timeStampWidth = [cell.timestamp.text sizeWithFont:cell.timestamp.font].width;
    CGFloat ksCompanyNameLableMaxWidth = 235;
    NSLog(@"timeStampWidth:%f", timeStampWidth);
    CGSize companyNameLableSize = CGSizeMake((ksCompanyNameLableMaxWidth - timeStampWidth), cell.companyNameLabel.frame.size.height);
    CGRect newFrame = cell.companyNameLabel.frame;
    newFrame.size = companyNameLableSize;
    cell.companyNameLabel.frame = newFrame;
    NSLog(@"companyLableWidth:%f", newFrame.size.width);


    return cell;
}

There are multiple problems with this code.

  1. As the table is initialised, although this piece of code is called for every cell. The width of the label is still set as the size in the story board. On the other side, if I scroll down, the label is displayed correctly as designed in the code.

  2. Because I also have a tabView controller within my app, whenever I switch between the tabs, the dynamically populated label width is getting updated with the default label width set in the storyboard again.

Could someone please tell me what I did wrong and suggest some solution?

Thanks

TypingPanda
  • 1,607
  • 3
  • 19
  • 32

3 Answers3

1

try this code

#define FONT_SIZE 14.0f
#define CELL_CONTENT_MARGIN 8.0f

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    NSString *text = [BugComments_text objectAtIndex:indexPath.row];
    CGSize constraint = CGSizeMake(tableView.frame.size.width - (CELL_CONTENT_MARGIN * 2), 20000.0f);
    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
    CGFloat height = MAX(size.height+40, 60.0f);
    return height + (CELL_CONTENT_MARGIN * 2);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSString *text = [arrayofdata objectAtIndex:indexPath.row];
     CGSize constraint = CGSizeMake(tableView.frame.size.width - (CELL_CONTENT_MARGIN * 2), 20000.0f);
     CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
     [cell.Content_Text setText:text];
     cell.Content_Text setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN+cell.Content_Email.frame.origin.y+cell.Content_Email.frame.size.height, tableView.frame.size.width - (CELL_CONTENT_MARGIN * 2), MAX(size.height,20.0f))];
}
Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
Muralikrishna
  • 1,044
  • 10
  • 17
  • Shaik & muralikrishna, I just need to adjust the label inside the cell not the height of the cell. I am facing something like what mentioned here [dynamic calculation of UILabel width in UITableViewCell](http://stackoverflow.com/questions/1947970/dynamic-calculation-of-uilabel-width-in-uitableviewcell) – TypingPanda Nov 06 '13 at 22:33
0

Try this in your cellForRowAtIndexPath for dynamically setting the company label text,

  CGSize textSize = {
        200.0,   // limit width
        20000.0  // and height of text area
    };

    CGSize contentSize = [yourText sizeWithFont:[UIFont systemFontOfSize:17.0] constrainedToSize:textSize lineBreakMode:NSLineBreakByWordWrapping];
    CGFloat contentHeight = contentSize.height < 36? 36: contentSize.height; // lower bound for height

    CGRect companyLabelFrame = [cell.companyNameLabel frame];
    companyLabelFrame.size.height = contentHeight;
    [cell.companyNameLabel setFrame:companyLabelFrame];
    cell.companyNameLabel setText:yourText];
karthika
  • 4,085
  • 3
  • 21
  • 23
0

I finally solve the problem by clearly specifying it again and get the answer from @rdelmar please look the link Here

Community
  • 1
  • 1
TypingPanda
  • 1,607
  • 3
  • 19
  • 32