0

UITableViewCell にある UITextView のサイズを変更しようとしています。それに応じて UITableViewCell のサイズも変更したいと思います。私の UITableViewCell は適切にサイズ変更されますが、UITextView は変更されません。正しいサイズにサイズ変更するのではなく、2 行だけになってしまいます。cellForRowAtIndexPath メソッドで初期化すると、次のようになります。

        UITextView *notesTextView = [[UITextView alloc] initWithFrame:CGRectMake(83, 12, TEXTVIEW_WIDTH, 22)]; 

        notesTextView.tag = TEXTVIEW_TAG;
        notesTextView.delegate = self;

        [cell.contentView addSubview:notesTextView];

textView デリゲート メソッドで変更しようとすると、次のようになります。

CGSize size = [textView.text sizeWithFont:textView.font constrainedToSize:CGSizeMake(TEXTVIEW_WIDTH, 460) lineBreakMode:UILineBreakModeWordWrap];
NSLog(@"size: %@", NSStringFromCGSize(size)); // has the correct size
if (size.height > self.notesRowHeight) {
    self.notesRowHeight = size.height;
    UITextView *aTextView = (UITextView *)[self.view viewWithTag:TEXTVIEW_TAG];
    CGRect textViewFrame = CGRectMake(aTextView.frame.origin.x, aTextView.frame.origin.y, TEXTVIEW_WIDTH, size.height);
    aTextView.frame = textViewFrame;
    aTextView.contentSize = size;
    [aTextView sizeToFit];
    [aTextView sizeThatFits:size];
    NSLog(@"%@", NSStringFromCGRect(aTextView.frame)); // has the correct height

したがって、最終的にはtableViewのサイズが適切に変更され、textViewフレームが正しいサイズ({{83、12}、{197、120}}など)であっても、textViewには2行しかありません。私が意図したように、UITableViewCellのサイズを取得することはありません。

編集: UITableViewCell ではない通常の UIView で UITextView のフレームのサイズを変更しようとすると、問題なく動作します。したがって、この場合の違いはわかりません。

4

4 に答える 4

4

私は答えを見つけました。行をリロードするのではなく、呼び出すだけです

[tableView beginUpdates];
[tableView endUpdates];

詳細については、以下を 参照してください。 UITableViewCell の UITextView スムーズな自動サイズ変更は、iPad ではキーボードを表示および非表示にしますが、iPhone では機能します

于 2012-05-29T21:26:16.697 に答える
1

私はこれを解決するための最良の方法を見つけました。

もちろん、最初にUITextViewを作成し、それをセルのcontentViewに追加します。「cellTextView」というUITextViewのインスタンス変数を作成しました。使用したコードは次のとおりです。

- (UITableViewCell *)tableView:(UITableView *)tableView fileNameCellForRowAtIndexPath:(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変数を作成し、initメソッドで変数を1に設定します。その後、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;
}
于 2013-02-14T20:03:32.500 に答える
0

I will give you an different view....

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
       .
       .
       if(_YOUR_ROW_ && _YOUR_SECTION_)
       {

           cell.textView.text = Text_Of_TextView;
       }
       .
       .

}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   .
   .
   if(_YOUR_ROW_ && _YOUR_SECTION_)
   {


       UIFont * FontSize = [UIFont systemFontOfSize:14.0];  
       CGSize textSize = [Text_Of_TextView sizeWithFont:FontSize constrainedToSize:CGSizeMake(Width_OF_textView, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

       return textSize.height+PADDING;
   }
   .
   .
}




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

    [Text_Of_TextView setText:textView.text];


 CGRect frame = textViewInRow.frame;
        frame.size.height = textViewInRow.contentSize.height;
        textViewInRow.frame = frame;
 [TableView_InView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_YOUR_ROW_ inSection:_YOUR_SECTION_]] withRowAnimation:UITableViewRowAnimationNone];

}

and also dont forget to implement auto resizing behaviour for textview

于 2012-05-29T04:28:21.917 に答える