2011-07-03 5 views
0

私は簡単なダイアリーアプリケーションを作成しようとしています。私はそれぞれの投稿を表示するShowPostTableViewControllerを持っています。私のテーブルビューでは、私は2つのセルを持つセクションを1つ持っています。ヘッドライン用とポストボディ用です。見出しは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は2回設定されているようですが、同じテキストの2つのレイヤーなので消去できません。それをどうすれば解決できますか?

私の元の質問に戻る私の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のサイズを変更する必要があります

- (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; 
} 

事はほとんど作品:

- (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; 

} 

私はこのような行の高さを設定します。ユーザーがリターンをヒットすると、テキストビューは拡大するように見えますが、14回しか動作しないので、カーソルがキーボードの後ろに隠れます。

これを解決する方法はありますか? //アンダース

EDIT

おかげで私はviewDidLoadメソッドに私のwillDisplayCell方法コードのほとんどを移動することで、問題を「見出しは二回設定されます」解決しました。そして私は、怒りの問題がどこにあるのかをローカライズしたと思います。私はそれが私のheightForRowAtIndexPathメソッドにあると思います。現在、次のようになっています:

arrayとそのコンテンツはviewDidLoadメソッドで設定されるため、サイズは変わりません。したがって、TextViewのコンテンツの後にも、セルのサイズを動的に増やす必要があると私は思っています。私はこのように書くときので:

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

セルのサイズが大きくなると、ユーザは、キーボードが邪魔になる前に、より多くの時間を「返す」打つことができます。ダイナミックにセルの高さを上げる方法はありますか?

答えて

1

私はそれを解決すると思っています。それは次のようになりますので、私は私のheightForRowAtIndexPathを更新

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

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のサイズを変更するのに時間がかかります –

関連する問題