2012-02-24 59 views
1

我使用使用自定義的UITableViewCell子類,NoteCell表視圖。子類有兩個文本標籤,一個名稱標籤和一個日期標籤,它們並排放置在單元格中。我的目標是讓名稱標籤自行調整,以便無論如何顯示完整日期。如何動態調整UITableViewCell中兩個文本標籤寬度的大小?

中的cellForRowAtIndexPath,我試圖計算和設定的兩個文本視圖的寬度,這樣的日期得到充分的展示和名字標籤被截斷。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"NoteCell"; 

    NoteCell *cell = (NoteCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    // Configure the cell... 
    cell.dateLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin; 

    cell.nameLabel.text = @"A name that should be truncated"; 
    cell.dateLabel.text = @"A long date to use as an example"; 

    // Set the widths 
    // First calculate how wide the date label needs to be. 
    cell.dateLabel.text = @"A really long date"; 
    CGSize dateLabelSize = [cell.dateLabel.text sizeWithFont:cell.dateLabel.font]; 
    CGFloat dateExpansionAmount = fabsf(dateLabelSize.width - cell.dateLabel.frame.size.width) + 10.0f; 
    cell.dateLabel.frame = CGRectMake(cell.dateLabel.frame.origin.x - dateExpansionAmount, 
             cell.dateLabel.frame.origin.y, 
             dateLabelSize.width, 
             cell.dateLabel.frame.size.height); 

    CGFloat nameLabelWidth = cell.dateLabel.frame.origin.x - cell.nameLabel.frame.origin.x; 
    cell.nameLabel.frame = CGRectMake(cell.nameLabel.frame.origin.x, 
             cell.nameLabel.frame.origin.y, 
             nameLabelWidth, 
             cell.nameLabel.frame.size.height); 
} 

不幸的是,因爲我不認爲我設置/正確計算幀邊界做的結果是不正確的。看到下面的圖片。

Image 1 Image 2

忽視的問題與框架計算是否有更好的方式來處理這個問題,或者是手動定位文本標籤框架的唯一途徑?

+0

您是否已嘗試在被問到的標籤上設置autoResize屬性? – 2012-02-24 23:33:55

+0

你的意思是說,設置autoresize屬性,即支柱和彈簧? - 如果是這樣,那麼當設備旋轉時,將調整標籤的大小以擴大或縮小。 – 2012-02-24 23:40:33

+0

您正確設置了日期標籤的調整大小掩碼,但未設置名稱標籤。它應該具有靈活的寬度和固定的右邊距。 – sosborn 2012-02-25 00:37:31

回答

2

嘗試使用它來設置你的dateLabel。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 

    static NSString *CellIdentifier = @"NoteCell"; 

    NoteCell *cell = (NoteCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    // Configure the cell... 
    cell.dateLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin; 

    cell.nameLabel.text = @"A name that should be truncated"; 
    cell.dateLabel.text = @"A long date to use as an example"; 

    //set frame cell.dateLabel 
    CGSize maximumSize = CGSizeMake(INT_MAX, 44); // CGSizeMake(width,height). 
    CGSize dateStringSize = [[cell.dateLabel.text sizeWithFont:[UIFont systemFontOfSize:cell.dateLabel.font] 
                  constrainedToSize:maximumSize 
                   lineBreakMode:UILineBreakModeWordWrap]; 

    CGRect dateFrame = cell.dateLabel.frame; 
    dateFrame.size.width = dateStringSize. width; 
    cell.dateLabel.frame = dateFrame; 


return cell; 
} 
+0

謝謝回覆! - 我已經嘗試過這種方法,但是會發生什麼情況是日期標籤從屏幕上偏離了披露指示符。基本上,因爲您正在增加寬度,而不是擴大標籤並將其向左擴展,則會將寬度增加到右側。 – 2012-02-26 01:11:54

+0

我也嘗試過使用支柱和彈簧。我將原點錨點設置在右上角,而不是默認的左上角。我也設置了標籤自動展開。仍然沒有運氣,因爲我遇到了與上述相同的問題。 – 2012-02-26 01:13:17

0

後您設置文本嘗試調用 方法「sizeToFit」 ,然後你可以得到正確的yourLabel.frame.size.width,所以如果2個標籤超過電池框架剛剛調整的名稱與標籤第二,開始在那裏的第一個結束,其小尺寸,並設置正確的位置...

新的編輯(見註釋)

cell.nameLabel.text = @"A name that should be truncated"; 
cell.dateLabel.text = @"A long date to use as an example"; 
[cell.nameLabel sizeToFit]; 
[cell.dateLabel sizeToFit]; 
// now check if the 2 width exceed cell size: 
if (cell.nameLabel.frame.size.width + cell.dateLabel.frame.size.width > cell.frame.size.width){ 
//resize nameText: 
cell.nameLabel.frame.size.width = cell.frame.size.width - cell.dateLabel.frame.size.width; 
// ...and maybe consider to give it a minim size requirement 
} 
// now set the positions 
cell.nameLabel.frame = CGRectMake(0,0,cell.nameLabel.frame.size.width,cell.nameLabel.frame.size.height); 
cell.dateLabel.frame = CGRectMake(cell.nameLabel.frame.size.width,0,cell.dateLabel.frame.size.width,cell.dateLabel.frame.size.height); 

PS 確保您autoresizingMask設置修正爲頂部和左側正確地爲兩個標籤

+0

我現在給它一個嘗試,但我注意到的第一件事是,'sizeToFit'方法調整大小和移動標籤出它創建它自己的一套問題的位置。你認爲有沒有更好的方法來實現這個目標,而不需要手動計算寬度? – 2012-02-25 00:09:07

+0

sizeToFit調整您的uilabel的框架以消除未使用的空間。這樣,您可以確切地知道有多大你的文字,和現在的「手動」計算其寬度也不是那麼難......看到新的編輯我的回答 – meronix 2012-02-25 10:18:16

+0

好,我給它一個嘗試,我很接近它來獲得工作 - 你的建議是有關sizeToFit。有一個徘徊的問題。標籤的起源是固定的,然後sizeToFit將擴大標籤的框架然而以適合文本(這是哪裏出了問題),它擴展了標籤沒有離開的權利,這意味着它跑出鏡頭過去披露的指標。標籤需要將寬度向左擴展,而不是右... – 2012-02-26 01:53:41

相關問題