0

我正在尝试根据同一行上另一个标签的内容动态设置标签宽度。

我正在“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;
}

此代码存在多个问题。

  1. 随着表的初始化,尽管对每个单元格都调用了这段代码。标签的宽度仍然设置为故事板中的大小。另一方面,如果我向下滚动,标签会按照代码中的设计正确显示。

  2. 因为我的应用程序中还有一个 tabView 控制器,所以每当我在选项卡之间切换时,动态填充的标签宽度都会再次更新为情节提要中设置的默认标签宽度。

有人可以告诉我我做错了什么并提出一些解决方案吗?

谢谢

4

3 回答 3

1

试试这个代码

#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))];
}
于 2013-11-06T05:20:41.320 回答
0

在您的 cellForRowAtIndexPath 中尝试此操作以动态设置公司标签文本,

  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];
于 2013-11-06T04:46:22.270 回答
0

我终于通过再次明确指定它来解决问题并从@rdelmar得到答案请看这里的链接

于 2013-11-07T07:00:21.367 回答