2012-01-11 91 views
11

我需要自定义单元格中UITableViewStyleGrouped table.It必须如下UITableViewCellStyleValue1如果为textLabel和detailTextLabel都没有截断显示,或作为UITableViewCellStyleSubtitle(但detailTextLabel宽度= contentView.frame.size.width和UITextAlignmentRight)如果值1样式之一的标签被截断。如何获取UITableViewCell的contentView宽度?

我需要知道方法- (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; 
} 
+0

您是否曾经为此找到过解决方案?我有一个类似的问题,在初始化期间,iPhone上的contentView.frame.size.width将返回320。 – cod3monk3y 2013-01-09 17:47:29

+1

我最终实现了'layoutSubviews'来调整我的自定义单元格内容。 – cod3monk3y 2013-01-09 18:01:09

+0

我做了同样的事情。 – Alexander 2013-01-10 05:08:31

回答

-2
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if (indexPath.section == 0) 
     return 60.0f; 
    else 
     return 50.0f; 

} 

通过应用此您可以设置单元格 - >内容查看。如果您需要动态单元格宽度,请使用上述方法中的标志

+0

我不明白你的意思。我不需要动态单元格宽度。我想知道cell.contentView宽度以了解:标签是否适合没有截断的单元格?如果没有,那么我想返回' - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath'其他高度。 – Alexander 2012-01-12 05:59:29

4

如果我没有弄错,heightForRowAtIndexPath:在单元格初始化之前或之后调用,所以没有时间调整大小。

这应该为你工作:

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

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

+1

此代码首先出320用于iPhone,750用于iPad,但真正的contentView宽度为300,iPhone和698为iPad(用于人像,宽度的屏幕),并且在我上下滚动表格之后,出现正确的值 – Alexander 2012-01-12 09:33:16

+0

在某些时候是正确的,但是从委托重新加载表是非常奇怪的,它负责重新加载;)每次它想要显示新的单元格时,我都必须重新加载表格,这些单元格还没有显示出来。 – Alexander 2012-01-13 10:59:30

+0

这是一个令人厌恶的解决方案,不得不重新加载整个表格视图来计算,这实在是不可接受的! – simonthumper 2015-12-16 10:43:34

0

UITableViewcontentView宽度小区的创建单元格时没有设置,所以你可以设置autoresizingMask的标签,就像这样:

label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 

对我来说,这是工作。