2011-07-03 26 views
0

我試圖創建一個簡單的日記應用程序。其中我有一個ShowPostTableViewController,我想要顯示每個帖子。在我的tableView中,我有一個部分有兩個單元格,一個用於標題,另一個用於帖子的主體。標題在UITextField中,而主體在UITextView中。我聲明都是這樣的:動態更改UITextView高度而不是滾動

UITextField * postHeadline; 
UITextView * postText; 

我在我的實現文件中綜合它們。我在willDisplayCell方法中設置了tableView的單元格。它看起來像這樣:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell 
forRowAtIndexPath:(NSIndexPath *)indexPath { 

    tableView.allowsSelection = NO; 

    CGRect wholeWindow = [self.view bounds]; 
    float headlineHeight = 40; 

    CGRect headlineRect = CGRectMake(10, 10, wholeWindow.size.width, headlineHeight); 

    CGRect bodyRect = CGRectMake(wholeWindow.origin.y, wholeWindow.origin.x, 
    wholeWindow.size.width, wholeWindow.size.height); 

    postHeadline = [[UITextField alloc] initWithFrame:headlineRect]; 
    postText = [[UITextView alloc] initWithFrame:bodyRect]; 

    NSString * headline = [[NSString alloc] initWithFormat:@"%@",[currentPostArray 
    objectAtIndex:0]]; 

    NSString * body = [[NSString alloc] initWithFormat:@"%@",[currentPostArray 
    objectAtIndex:1]]; 

    postHeadline.text = headline; 

    postHeadline.font = [UIFont fontWithName:@"Helvetica-Bold" size:24.0]; 

    postText.text = body; 
    postText.backgroundColor = [UIColor colorWithRed:254.0/255.0 green:255.0/255.0 
     blue:237.0/255.0 alpha:1]; 
    postText.font = [UIFont fontWithName:@"Georgia" size:17.0]; 
    postText.scrollEnabled = NO; 
    postText.delegate = self; 

    if (indexPath.row == 0) { 
     [cell.contentView addSubview:postHeadline]; 
    } 
    if (indexPath.row == 1) { 
     [cell.contentView addSubview:postText]; 
     CGRect frame = postText.frame; 
     frame.size.height = postText.contentSize.height; 
     postText.frame = frame; 
    } 
} 

是的,我是一個初學者,可能會像瘋了似的泄漏內存。與此相關的問題。看起來像我的UITextField postHeadline被設置兩次,我不能抹去它,因爲它是兩層相同的文本。我該如何解決這個問題?

回到我原來的問題。我的cellForRowAtIndexPath保持完好無損。我已經坐在我的UITextView的委託方法(認爲這是問題所在)。我發現了一個幾乎在這裏工作的解決方案:UITextView change height instead of scroll

他們看起來像這樣:

- (void)textViewDidBeginEditing:(UITextView *)textView 
{ 
    textView.frame =CGRectMake(textView.frame.origin.x,textView.frame.origin.y,textView. 
frame.size.width,textView.frame.size.height + 100); 

} 

這個方法應該調整的TextView:

- (void)textViewDidChange:(UITextView *)textView 
{ 

    CGFloat fontHeight = (textView.font.ascender - textView.font.descender) + 1; 

    CGRect newTextFrame = textView.frame; 
    newTextFrame.size = textView.contentSize; 
    newTextFrame.size.height = newTextFrame.size.height + fontHeight; 
    textView.frame = newTextFrame; 

} 

我設置行的高度是這樣的:

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

    NSString * body = [[NSString alloc] initWithFormat:@"%@",[currentPostArray 
objectAtIndex:1]]; 

    CGSize bodySize = [body sizeWithFont:[UIFont fontWithName:@"Georgia" size:17.0] 
constrainedToSize:CGSizeMake(self.view.frame.size.width,CGFLOAT_MAX)]; 

    if (indexPath.section == 0 && indexPath.row == 1) { 
     return bodySize.height + 100; 
    }  
    return 50; 
} 

事情幾乎工作。當用戶點擊返回時,textview似乎展開,但它只能運行14次,然後光標隱藏在鍵盤後面。

任何想法和技巧如何解決這將是偉大的!

感謝 //安德斯

編輯

我解決了通過移動我的大多數willDisplayCell方法的代碼,以我的viewDidLoad方法「的標題被設定兩次」的問題。而且我認爲我已經將本地化問題歸咎於這個問題。我認爲這是在我的heightForRowAtIndexPath方法。目前,它看起來像這樣:

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

    NSString * body = [[NSString alloc] initWithFormat:@"%@",[currentPostArray 
objectAtIndex:1]]; 

    CGSize bodySize = [body sizeWithFont:[UIFont fontWithName:@"Georgia" size:17.0] 
constrainedToSize:CGSizeMake(self.view.frame.size.width,CGFLOAT_MAX)]; 

    if (indexPath.section == 0 && indexPath.row == 1) { 
     return bodySize.height + 100; 
    }  
    return 50; 
} 

,數組和它的內容獲取viewDidLoad方法設置,大小保持不變。因此,我認爲我需要在TextView的內容之後動態增加單元格的大小。因爲當我寫這樣的:

if (indexPath.section == 0 && indexPath.row == 1) { 
    return 1000; 
} 

電池的尺寸增大,並且用戶可以敲擊鍵盤的方式得到前「迴歸」多次。是否有辦法動態增加到細胞的高度?

回答

1

想我解決了它。這些方法做了魔術:

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

我更新了我的heightForRowAtIndexPath所以它看起來是這樣的:

if (indexPath.section == 0 && indexPath.row == 1) {   
    return postText.frame.size.height + headlineHeight; 
}  
return 44; 

我添加了這些方法viewDidChange和viewWillAppear中(重設大小)。

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

這是非常多的。

+0

當我使用您的方法表數據更新變得非常緩慢,它需要更多時間來調整textview的大小 –