2011-10-04 57 views
8

我已經對這個問題感到沮喪了幾天。我試圖添加一個UILabel到UITableViewCell。我希望UILabel跨越單元格的整個寬度,在左右兩側減去5或10以獲得外觀。我的問題是以編程方式確定放置標籤的單元格框架的大小。無論使用哪個UITableViewStyle,cell.contentVew.frame.size.width值都不在單元框架本身的寬度附近。檢索UITableViewCell框架

例如,在我構建的表,我可以實現通過繼承的UITableViewCell和帶手動確定寬度創建一個UILabel(通過公正的審判和錯誤)經我想要的結果:

CGRectMake(10, 12, 397, self.contentView.frame.size.height); 

但它的那令我煩惱的397號碼。我想要一種以編程方式確定它應該用於任何寬度表格或樣式的方法。這應該是一個簡單的過程,只需確定整個單元格的寬度,然後減去10或20,這樣UILabel的邊緣實際上不會觸及單元格的邊緣。

但是,如果我設置tableViewStyle到UITableViewStyleDefault然後嘗試:

NSLog(@"Width: %f", self.contentView.frame.size.width); 

我得到320。如果我的樣式設置爲任何其他三種風格,返回的數字是302甚至320數字不是靠近單元格框架的寬度(與我手動確定的397個數字一樣)。

我需要訪問什麼值才能返回單元格繪圖框架的整個寬度?我相信,就像最令人頭痛的問題一樣,這個解決方案會讓我想要額頭上的自拍,但我現在已經準備好了。

編輯更多信息:

對任何有興趣的人一個澄清。我的這個問題主要涉及分組樣式表。

CGFloat cellWidth = [tableView rectForRowAtIndexPath:indexPath].size.width; 

時遇到的問題是,rectForRowAtIndexPath方法返回,其中所述細胞是框架的寬度:對於一個普通的風格,回答我的問題上述能夠簡單地cellForRowAtIndexPath方法中通過確定由於單元格寬度是框架的整個寬度,所以對於普通樣式表格來說這很好。但是,在分組表格中,單元格的寬度小於繪製框架的寬度,因此此方法將返回一個比單元格寬度稍寬的數字。組合表格樣式中的單元格的寬度可能小於表格框架的寬度,因此這可能是解決問題的方法。如果是這種情況,我會調查並回答我自己的問題。

+0

偉大的問題我得到完全相同的行爲,它驅使我堅果。我的解決方法是不考慮幀大小,只是參考內容視圖大小百分比的子視圖寬度。我很確定內容視圖框架排除了爲附件保留的單元格的左側和右側區域(移動,刪除,披露等),但仍然沒有回答爲什麼單元格框架總是報告錯誤大小的問題。希望有人會來,並理解這一切。 – Rog

+0

這是在風景? –

+0

@Bartosz - 在我的例子中,我從肖像中獲取數字。 – Chris

回答

8

我已經確定了我自己的答案,我希望它能幫助任何人面臨同樣的問題。一個分組的tableView的餘量的計算我上this StackOverflow answer.

此代碼發現將提供跨越與所述單元的兩個邊緣之間的裕度的小區的的tableView小區內的標籤,並在細胞內垂直居中。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    UILabel *label; 
    CGFloat groupedStyleMarginWidth, tableViewWidth; 
    UIFont *labelFont = [UIFont boldSystemFontOfSize:17.0]; // Set to whatever you like 
    NSString *labelText = @"Test String"; 

    // Calculate the margin between the cell frame and the tableView 
    // frame in a grouped table view style. 
    tableViewWidth = tableView.frame.size.width; 
    if (tableView.style == UITableViewStyleGrouped) { 
     if (tableViewWidth > 20) 
      groupedStyleMarginWidth = (tableViewWidth < 400) ? 10 : MAX(31, MIN(45, tableViewWidth*0.06)); 
     else 
      groupedStyleMarginWidth = tableViewWidth - 10; 
    } 
    else 
     groupedStyleMarginWidth = 0.0; 

    if (cell == nil) { 
     CGRect tableViewRect; 
     CGRect labelRect; 
     CGFloat x, y, w, h, labelMargin; 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

     // Retrieve the rect of the table view. 
     tableViewRect = [tableView rectForRowAtIndexPath:indexPath]; 

     // Set whatever margin around the label you prefer. 
     labelMargin = 10; 

     // Determine rect values for the label. 
     x = tableRect.origin.x + labelMargin; 

     // Calculate width of label 
     w = tableRect.size.width - (groupedStyleMargin * 2) - (labelMargin * 2); 

     // Calculate height of table based on font set earlier. 
     h = [labelText sizeWithFont:font].height; 

     // Calculate y position for the label text baseline to center 
     // vertically within the cell. 
     y = (tableRect.origin.y/2) - (h/4); 

     labelRect = CGRectMake(x, y, w, h); 

     label = [[UILabel alloc] initWithFrame:labelRect]; 
     label.text = labelText; 
     label.tag = 0; 
     [cell.contentView addSubview:stepLabel]; 
     [label release]; 
    } 
    else { 
     label = (UILabel *)[cell viewWithTag:0]; 
    } 
1

這樣的聲音最好現在由自動佈局約束來處理。