【问题标题】:How get contentView width of UITableViewCell?如何获取 UITableViewCell 的 contentView 宽度?
【发布时间】:2012-01-11 12:48:44
【问题描述】:

我需要在 UITableViewStyleGrouped 表中创建自定义单元格。如果 textLabel 和 detailTextLabel 没有截断显示,它必须看起来像 UITableViewCellStyleValue1,或者像 UITableViewCellStyleSubtitle 一样(但有 detailTextLabel width=contentView.frame.size.width和 UITextAlignmentRight) 如果在 Value1 样式中,其中一个标签被截断。

我需要知道方法- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 中cell.contentView 的宽度来决定单元格的高度。我比较 cell.contentView.width 和标签宽度的总和(我使用方法sizeThatFits: 得到它们)来决定它。

那么,如何在单元格创建之前获取正确的 cell.contentView.frame.size.width?

UPD:一些代码来澄清问题。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    CGFloat cellHeight = 44;

    CGSize textSize = [[arrayOfTextData objectAtIndex:indexPath.row] 
                           sizeWithFont:[UIFont systemFontOfSize:14]
                           constrainedToSize:CGSizeMake( 1000, 100)
                           lineBreakMode:UILineBreakModeCharacterWrap];
    CGSize detailedTextSize = [[arrayOfDetailTextData objectAtIndex:indexPath.row] 
                                    sizeWithFont:[UIFont systemFontOfSize:14]
                                    constrainedToSize:CGSizeMake(1000, 100) 
                                    lineBreakMode:UILineBreakModeCharacterWrap];
    if (textSize.width + detailedTextSize.width > /* I need here cell.contentView.frame.size.width*/  300) {
         cellHeight = textSize.height + detailedTextSize.height;
    }
    return cellHeight;
}

UPD2: 问题是当我创建单元格时 cell.contentView.frame.size.width 等于 cell.frame.size.width 等于 320,但是在单元格之后出现了 cell.contentView。 frame.size.width 变化依赖于 tableView 宽度和 tableView 样式。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"tmpCell";
    CustomTableViewCellValue1OrSubtitle *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[CustomTableViewCellValue1OrSubtitle alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];     
    } 
    //--Configure the cell
        cell.textLabel.text = [arrayOfTextData objectAtIndex:indexPath];
        cell.detailTextLabel.text = [arrayOfDetailTextData objectAtIndex:indexPath.row];

    [cell layoutIfNeeded];

    CGSize textSize = [cell.textLabel.text sizeWithFont:cell.textLabel.font constrainedToSize:CGSizeMake(cell.contentView.bounds.size.width, 99999) lineBreakMode:cell.textLabel.lineBreakMode];
    CGSize detailedTextSize = [cell.detailTextLabel.text sizeWithFont:cell.textLabel.font constrainedToSize:CGSizeMake(cell.contentView.bounds.size.width, 99999) lineBreakMode:cell.textLabel.lineBreakMode];
    CGFloat cellHeight = cell.contentView.frame.size.height;

    NSLog(@"contentView.frame.size.width = %f",cell.contentView.frame.size.width);
    //out: contentView.frame.size.width = 320.0
    //Always print 320, even if I set Simulator on iPad

    if (textSize.width + detailedTextSize.width > cell.contentView.frame.size.width) {
        cellHeight = textSize.height + detailedTextSize.height;
    }
    return cellHeight;
}

【问题讨论】:

  • 您找到解决方案了吗?我有一个类似的问题,contentView.frame.size.width 在初始化期间在 iPhone 上返回 320。
  • 我最终实现了layoutSubviews 来调整我的自定义单元格内容的大小。
  • 我也做过同样的事情。
  • 您介意回答问题并将您的答案标记为正确吗?还是layoutSubviews 是您解决特定问题唯一需要做的事情?如果您不想要它,我很乐意得到答案:D

标签: objective-c ios uitableview


【解决方案1】:

如果我没记错的话,heightForRowAtIndexPath: 在单元格初始化之前或之后被调用,因此没有时间调整它的大小。

这应该适合你:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%f", cell.contentView.frame.size.width);
}

不幸的是,这是在 heightForRowAtIndexPath 之后调用的。我发现根据非常量宽度调整高度的唯一方法是在第一次调用此方法时为宽度设置一个实例变量,然后重新加载表格视图并使用 heightForRowAtIndexPath 中的宽度实例变量。

【讨论】:

  • 这段代码最初是 iPhone 的 320,iPad 的 750,但真正的 contentView 宽度对于 iPhone 是 300,对于 iPad 是 698(对于纵向,屏幕宽度),并且在我向上滚动表格后正确的价值观
  • 您在某些时候是对的,但是从负责重新加载的代表重新加载表格很奇怪;)每次要显示新单元格时我都必须重新加载表格,这还没有显示。
  • 这是一个令人作呕的解决方案,不得不重新加载整个表格视图来计算这实在是不可接受的!
【解决方案2】:

创建单元格时未设置UITableViewStyleGrouped UITableView 中单元格的contentView 宽度,因此您可以为标签设置autoresizingMask,如下所示:

label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

对我来说它是有效的。

【讨论】:

    【解决方案3】:
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
         if (indexPath.section == 0) 
             return 60.0f;
        else
            return 50.0f;
    
    }
    

    通过应用这个你可以设置单元格 -> contentView。如果您需要动态单元格宽度,请使用上述方法中的标志

    【讨论】:

    • 我不明白你的意思。我不需要动态单元格宽度。我想知道 cell.contentView 宽度以了解:标签是否适合不截断的单元格?如果没有,那么我想以- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 其他高度返回。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    相关资源
    最近更新 更多