2012-08-13 51 views
1

在我的情況下,我將如何調整UITableViewCell以適應它包含的UITextView的高度?如何根據子視圖正確調整父視圖的大小? (例如,包含UITextView的UITableViewCell)

我目前正在努力實現這個使用下面的代碼:

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

    // init the font that we use 
    UIFont *font = [UIFont fontWithName:@"SourceSansPro-Regular" size:16]; 

    if (indexPath.section == 0) { 

     // get the text size of the thread 
     CGSize textSize = [_thread.text sizeWithFont:font constrainedToSize:CGSizeMake(280, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap]; 

     return textSize.height + 20; 

    } 

    // default 
    return 60; 

} 

我覺得如果我能得到這個方法訪問UITableViewCell,然後我可以使用UITextViewcontentSize財產在其中,並返回。但我認爲在執行流程的這一點上這是不可能的。

我不能只是調整緩衝區,因爲它不會縮放get size函數返回的文本大小。文字越短,緩衝區的大小越大。

這是調整UITableViewCell的高度,但它沒有在其中顯示UITextView。我已經在sizeWithFont:constrainedToSize:lineBreakMode:方法中指定UITextView的寬度,即280。我已經指定我希望基本上沒有最大高度,以便它可以使用常量來儘可能多地進行文字包裝。我將20單元的緩衝區添加到方法調用的結果高度,因爲在UITextView之上有一個20單元緩衝區,在我的Storyboard中,我還想在其下面有一個20單元緩衝區。儘管如此,所有這些的最終結果仍然有文本被剪掉。下面是我在談論的一個畫面:

clipped cell contents

任何想法?謝謝!

回答

1

我打算繼續回答我的問題。對此的回答分兩步進行。

首先,在tableView:cellForRowAtIndexPath:方法的內部邏輯期間調整UITextView的大小。請確保在對UITextView中的文字進行任何更改後執行此操作。我使用下面的方法重新大小我UITextView,裏面的UITableViewCell一個子類,其中self.text是文本觀點:

- (void)resizeTextView { 

    // get the current frame size 
    CGRect frame = self.text.frame; 

    // set it to the height of the text view 
    frame.size.height = self.text.contentSize.height; 

    // set the new size of the text view 
    self.text.frame = frame; 

} 

接下來,我指定的tableView:heightForRowAtIndexpath:方法內的UITableViewCell的高度。這就是大部分神祕的地方,因爲爲了獲得這一行的高度,你必須計算將要在行中顯示的任何文本的高度,以及計算高度所需的任何其他內容。在我的情況下,是一個UITextView。如果不能訪問此方法中的UITableViewCell實例,則不得不使用類似sizeWithFont:constrainedToSize:lineBreakMode:之類的內容。但是,我注意到這並沒有給我帶來一致的高度。或者至少,不是高度似乎爲UITextView我的文字留下了足夠的空間。雖然這是問題。文本視圖具有附加的左右填充以及不同的線高度,或者與sizeWithFont方法使用的不同。

所以,捏造sizeWithFont方法的結果的一段時間後,我結束了以下代碼:

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

    // init the font that we use 
    UIFont *font = [UIFont fontWithName:@"SourceSansPro-Regular" size:16]; 

    if (indexPath.section == 0) { 

     // get the text size of the thread 
     CGSize textSize = [_thread.text sizeWithFont:font constrainedToSize:CGSizeMake(280 - 16, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap]; 

     // text height + padding 
     return textSize.height + 40; 

    } else if (indexPath.section == 1) { 

     // get the comment for this row 
     Comment *comment = [_comments objectAtIndex:indexPath.row]; 

     // get the heights of the text areas 
     CGSize textSize = [comment.text sizeWithFont:font constrainedToSize:CGSizeMake(280 - 16, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap]; 

     return textSize.height + 40; 

    } 

    return 60; 

} 

在尺寸約束參數,我減去16個單位,因爲UITextView具有大約一個填充左側和右側共有8個單位。這將使我們更接近我們對文本高度的期望。

我還添加了一些填充到40個單位的最終高度,因爲該行本身需要在UITextView周圍填充一些填充。

你走了!希望有所幫助。

相關問題