2010-09-20 94 views
13

我有一個UITextView在UITableViewCell contentview中,並允許單元格自動調整大小,以便輸入的文本完全顯示 - 我試圖完成的是自動調整單元格,如本機iOS4通訊錄應用程序有,當你輸入聯繫人的「筆記」 - 即當textView的contentSize改變 - 我調用reloadRowsAtIndexPaths和在委託的heightForRowAtIndexPath我提供了行的新高度 - 這是做的工作,但它不是很好,光滑像聯繫人的應用程序 - 我幾乎可以肯定,蘋果公司在該應用程序中使用了一些未公開的技巧,使得單元格的contentView無需調用reloadRowsAtIndexPaths就可以平滑和動畫。我的問題是你會如何建議實現這樣的功能?我希望我沒有錯過任何細節的解釋。在UITableViewCell中的UITextView平滑自動調整大小

+0

您是否嘗試過使用的UIView的動畫?我從來沒有用過它,但這可能會起作用。 – Jukurrpa 2010-09-20 11:28:09

+0

是的,我已經嘗試了內部內容的一些UIView動畫,它有點平滑的過程,但我想知道是否有一種方法不使用reloadRowsAtIndexPaths,似乎沒有暴露的方法。 – justadreamer 2010-09-20 12:44:31

回答

15

請嘗試下面的代碼,這將有所幫助。您不必使用reloadRowsAtIndexPaths等任何重新加載函數。

// TextView的委託

- (void)textViewDidChange:(UITextView *)textView { 
    if (contentView.contentSize.height > contentRowHeight) { 

    contentRowHeight = contentView.contentSize.height; 

    [theTableView beginUpdates]; 
    [theTableView endUpdates]; 

    [contentView setFrame:CGRectMake(0, 0, 300.0, contentView.contentSize.height)]; 
    } 
} 

//的tableview委託

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

    if (indexPath.row == 0) 
     height = kTitleRowHeight; 
    else 
     height = contentRowHeight; 

    return height; 
} 
+0

此代碼中的contentView是什麼?我認爲它是textView? – justadreamer 2010-10-10 11:42:26

+0

這是UITableViewCell的屬性。 UITextView添加在此屬性中。 [cell.contentView addSubview:textView]; – 2010-10-10 12:03:27

+0

我在我的應用程序中使用這個解決方案,它在iPhone上工作100%,但在iPad上它不是:當用戶輸入多行文本時,「UITableViewCell」會變高,因爲它應該是,但是'UITextView'實際上會移動* up *,所以第一行文本在UITableViewCell的頂部之上 - 並且被隱藏。任何想法如何在iPad上解決這個問題? – Jason 2010-10-24 07:18:59

11

我發現解決這個的最好方法。

首先,當然,你要創建你的UITextView並將它添加到你的單元格的contentView。我創建的UITextView的實例變量稱爲「cellTextView」這裏是我使用的代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; 

    if (!cellTextView) { 
     cellTextView = [[UITextView alloc] initWithFrame:CGRectMake(5.0, 5.0, cell.bounds.size.width - 30.0, cell.bounds.size.height - 10.0)]; // I use these x and y values plus the height value for padding purposes. 
    } 
    [cellTextView setBackgroundColor:[UIColor clearColor]]; 
    [cellTextView setScrollEnabled:FALSE]; 
    [cellTextView setFont:[UIFont boldSystemFontOfSize:13.0]]; 
    [cellTextView setDelegate:self]; 
    [cellTextView setTextColor:[UIColor blackColor]]; 
    [cellTextView setContentInset:UIEdgeInsetsZero]; 
    [cell.contentView addSubview:cellTextView]; 

    return cell; 
} 

然後,創建一個名爲numberOfLines一個int變量,設置變量爲1在您的init方法。之後,您textViewDelegate的textViewDidChange方法,使用此代碼:

- (void)textViewDidChange:(UITextView *)textView 
{ 
    numberOfLines = (textView.contentSize.height/textView.font.lineHeight) - 1; 

    float height = 44.0; 
    height += (textView.font.lineHeight * (numberOfLines - 1)); 

    CGRect textViewFrame = [textView frame]; 
    textViewFrame.size.height = height - 10.0; //The 10 value is to retrieve the same height padding I inputed earlier when I initialized the UITextView 
    [textView setFrame:textViewFrame]; 

    [self.tableView beginUpdates]; 
    [self.tableView endUpdates]; 

    [cellTextView setContentInset:UIEdgeInsetsZero]; 
}  

最後,這段代碼粘貼到您的heightForRowAtIndexPath方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    float height = 44.0; 
    if (cellTextView) { 
     height += (cellTextView.font.lineHeight * (numberOfLines - 1)); 
    } 
    return height; 
} 
+0

+1這效果很好。 – 2013-06-10 01:08:19

+1

是的,這個作品很棒+1。另外,通過將indexOfLines存儲在index = indexPath.row處的NSArray中,將textView.tag設置爲indexPath.row,然後使多個單元格中的多個UITextviews隨每個單元格中的文本大小而變化。 – Rambatino 2013-06-15 15:38:17

+0

謝謝!一個簡單的解決辦法是找到一個不同的方式來確定你想調整哪個textView。此代碼是爲每個單元格使用一個textView而設計的。 – 2013-06-17 18:04:45