2012-01-11 58 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; 

對我來說,這是工作。