121

iOS 7 で UITextView を含む UITableViewCell の高さを計算するにはどうすればよいですか?

同様の質問に対して多くの回答が見つかりましたがsizeWithFont:、すべてのソリューションに参加しており、この方法は推奨されていません!

使用する必要があることはわかっています- (CGFloat)tableView:heightForRowAtIndexPath:が、TextView がテキスト全体を表示するために必要な高さを計算するにはどうすればよいですか?

4

12 に答える 12

428

まず、テキストのレンダリング方法に関して、UITextView と UILabel には大きな違いがあることに注意することが非常に重要です。UITextView はすべての境界線にインセットがあるだけでなく、その中のテキスト レイアウトもわずかに異なります。

したがって、sizeWithFont:UITextViews を使用するのは悪い方法です。

代わりに、指定できる境界ボックス内のすべてのコンテンツを表示するために必要な最小サイズを返すUITextView関数が呼び出されます。sizeThatFits:UITextView

以下は、iOS 7 とそれ以前のバージョンの両方で同等に機能し、現在のところ非推奨のメソッドは含まれていません。


シンプルなソリューション

- (CGFloat)textViewHeightForAttributedText: (NSAttributedString*)text andWidth: (CGFloat)width {
    UITextView *calculationView = [[UITextView alloc] init];
    [calculationView setAttributedText:text];
    CGSize size = [calculationView sizeThatFits:CGSizeMake(width, FLT_MAX)];
    return size.height;
}

この関数は、NSAttributedStringと目的の幅を として取り、CGFloat必要な高さを返します。


詳細なソリューション

私は最近似たようなことをしたので、私が遭遇した接続された問題の解決策もいくつか共有したいと思いました. それが誰かを助けることを願っています。

これははるかに詳細で、次の内容をカバーします。

  • もちろんUITableViewCell、含まれている の内容全体を表示するために必要なサイズに基づいての高さを設定するUITextView
  • テキストの変更に対応する (および行の高さの変更をアニメートする)
  • カーソルを表示領域内に保持し、編集中UITextViewにサイズを変更するときにファーストレスポンダを保持するUITableViewCell

静的なテーブル ビューを使用している場合、または既知の数の しかない場合は、UITextViewステップ 2 をより簡単にできる可能性があります。

1. まず、heightForRowAtIndexPath を上書きします。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // check here, if it is one of the cells, that needs to be resized
    // to the size of the contained UITextView
    if (  )             
        return [self textViewHeightForRowAtIndexPath:indexPath];
    else
    // return your normal height here:
            return 100.0;           
}

2. 必要な高さを計算する関数を定義します。

NSMutableDictionary(この例では と呼ばれるtextViews) をインスタンス変数としてUITableViewControllerサブクラスに追加します。

UITextViewsこの辞書を使用して、次のように個人への参照を保存します。

(そして、はい、indexPaths は辞書の有効なキーです)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    // Do you cell configuring ...

    [textViews setObject:cell.textView forKey:indexPath];
    [cell.textView setDelegate: self]; // Needed for step 3

    return cell;
}

この関数は実際の高さを計算します:

- (CGFloat)textViewHeightForRowAtIndexPath: (NSIndexPath*)indexPath {
    UITextView *calculationView = [textViews objectForKey: indexPath];
    CGFloat textViewWidth = calculationView.frame.size.width;
    if (!calculationView.attributedText) {
        // This will be needed on load, when the text view is not inited yet
        
        calculationView = [[UITextView alloc] init];
        calculationView.attributedText = // get the text from your datasource add attributes and insert here
        textViewWidth = 290.0; // Insert the width of your UITextViews or include calculations to set it accordingly
    }
    CGSize size = [calculationView sizeThatFits:CGSizeMake(textViewWidth, FLT_MAX)];
    return size.height;
}

3.編集中のサイズ変更を有効にする

次の 2 つの関数では、 のデリゲートが にUITextViews設定されていることが重要UITableViewControllerです。デリゲートとして何か他のものが必要な場合は、そこから関連する呼び出しを行うか、適切な NSNotificationCenter フックを使用することで回避できます。

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

    [self.tableView beginUpdates]; // This will cause an animated update of
    [self.tableView endUpdates];   // the height of your UITableViewCell

    // If the UITextView is not automatically resized (e.g. through autolayout 
    // constraints), resize it here

    [self scrollToCursorForTextView:textView]; // OPTIONAL: Follow cursor
}

4.編集中にカーソルをたどる

- (void)textViewDidBeginEditing:(UITextView *)textView {
    [self scrollToCursorForTextView:textView];
}

UITableViewUITableView の可視 Rect 内にない場合、カーソルの位置までスクロールします。

- (void)scrollToCursorForTextView: (UITextView*)textView {
    
    CGRect cursorRect = [textView caretRectForPosition:textView.selectedTextRange.start];
    
    cursorRect = [self.tableView convertRect:cursorRect fromView:textView];
    
    if (![self rectVisible:cursorRect]) {
        cursorRect.size.height += 8; // To add some space underneath the cursor
        [self.tableView scrollRectToVisible:cursorRect animated:YES];
    }
}

5.インセットを設定して、可視四角形を調整します

編集中UITableView、キーボードの一部が隠れる場合があります。テーブルビューのインセットが調整されていないscrollToCursorForTextView:場合、カーソルがテーブルビューの下部にある場合、カーソルまでスクロールできません。

- (void)keyboardWillShow:(NSNotification*)aNotification {
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(self.tableView.contentInset.top, 0.0, kbSize.height, 0.0);
    self.tableView.contentInset = contentInsets;
    self.tableView.scrollIndicatorInsets = contentInsets;
}

- (void)keyboardWillHide:(NSNotification*)aNotification {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.35];
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(self.tableView.contentInset.top, 0.0, 0.0, 0.0);
    self.tableView.contentInset = contentInsets;
    self.tableView.scrollIndicatorInsets = contentInsets;
    [UIView commitAnimations];
}

そして最後の部分:

ビューがロードされた内部で、次の方法でキーボードの変更の通知にサインアップしますNSNotificationCenter

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

この回答をとても長くしたことで、私に腹を立てないでください。質問に答えるためにすべてが必要なわけではありませんが、これらの直接関連する問題が役立つ人が他にもいると思います.


アップデート:

Dave Haupert が指摘したように、rectVisible関数を含めるのを忘れていました。

- (BOOL)rectVisible: (CGRect)rect {
    CGRect visibleRect;
    visibleRect.origin = self.tableView.contentOffset;
    visibleRect.origin.y += self.tableView.contentInset.top;
    visibleRect.size = self.tableView.bounds.size;
    visibleRect.size.height -= self.tableView.contentInset.top + self.tableView.contentInset.bottom;
    
    return CGRectContainsRect(visibleRect, rect);
}

scrollToCursorForTextView:また、プロジェクトの TextFields の 1 つへの直接参照がまだ含まれていることに気付きました。bodyTextView見つからないという問題がある場合は、機能の更新バージョンを確認してください。

于 2013-09-15T22:16:56.960 に答える
10

UITableViewAutomaticDimension を使用している場合、非常に単純な (iOS 8 のみ) ソリューションがあります。私の場合、それは静的なテーブルビューですが、これを動的なプロトタイプに適応させることができると思います...

テキストビューの高さの制約アウトレットがあり、次のようなメソッドを実装しました:

// Outlets

@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textViewHeight;


// Implementation

#pragma mark - Private Methods

- (void)updateTextViewHeight {
    self.textViewHeight.constant = self.textView.contentSize.height + self.textView.contentInset.top + self.textView.contentInset.bottom;
}

#pragma mark - View Controller Overrides

- (void)viewDidLoad {
    [super viewDidLoad];
    [self updateTextViewHeight];
}

#pragma mark - TableView Delegate & Datasource

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

#pragma mark - TextViewDelegate

- (void)textViewDidChange:(UITextView *)textView {
    [self.tableView beginUpdates];
    [self updateTextViewHeight];
    [self.tableView endUpdates];
}

ただし、覚えておいてください: テキスト ビューはスクロール可能である必要があり、自動寸法で機能するように制約を設定する必要があります。

  • セル内のすべてのビューを相互に関連させて設定します。高さは固定されています (プログラムで変更するテキスト ビューの高さを含みます)。
  • 最上部のビューには上部の間隔があり、最下部のビューにはスーパー ビューに対する下部の間隔があります。

最も基本的なセルの例は次のとおりです。

  • テキストビュー以外のビューはセルにありません
  • テキスト ビューのすべての辺の余白が 0 で、テキスト ビューの事前定義された高さの制約。
于 2015-04-08T12:04:41.153 に答える
4

シンプルさと迅速なプロトタイピングを目的としたもう 1 つのソリューションを次に示します。

設定:

  1. プロトタイプ セルを含むテーブル。
  2. 各セルには、動的サイズUITextViewの他のコンテンツが含まれています。
  3. プロトタイプ セルは に関連付けられていTableCell.hます。
  4. UITableViewに関連付けられていTableViewController.hます。

解決:

(1) に追加TableViewController.m:

 // This is the method that determines the height of each cell.  
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    // I am using a helper method here to get the text at a given cell.
    NSString *text = [self getTextAtIndex:indexPath];

    // Getting the height needed by the dynamic text view.
    CGSize size = [self frameForText:text sizeWithFont:nil constrainedToSize:CGSizeMake(300.f, CGFLOAT_MAX)];

    // Return the size of the current row.
    // 80 is the minimum height! Update accordingly - or else, cells are going to be too thin.
    return size.height + 80; 
}

// Think of this as some utility function that given text, calculates how much 
// space would be needed to fit that text.
- (CGSize)frameForText:(NSString *)text sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
{
    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                          font, NSFontAttributeName,
                                          nil];
    CGRect frame = [text boundingRectWithSize:size
                                      options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                   attributes:attributesDictionary
                                      context:nil];

    // This contains both height and width, but we really care about height.
    return frame.size;
}

// Think of this as a source for the text to be rendered in the text view. 
// I used a dictionary to map indexPath to some dynamically fetched text.
- (NSString *) getTextAtIndex: (NSIndexPath *) indexPath
{
    return @"This is stubbed text - update it to return the text of the text view.";
}

(2) に追加TableCell.m:

// This method will be called when the cell is initialized from the storyboard
// prototype. 
- (void)awakeFromNib
{
    // Assuming TextView here is the text view in the cell. 
    TextView.scrollEnabled = YES;
}

説明:

ここで起こっていることは次のとおりです。各テキスト ビューは、垂直および水平の制約によってテーブル セルの高さにバインドされています。つまり、テーブル セルの高さが増加すると、テキスト ビューのサイズも増加します。@manecosta のコードの修正版を使用して、セル内の特定のテキストに合わせてテキスト ビューに必要な高さを計算しました。つまり、X 文字のテキストを指定すると、テキスト ビューの必要な高さに一致frameForText:するプロパティを持つサイズが返されます。size.height

あとは、必要なテキスト ビューの高さに合わせてセルの高さを更新するだけです。そして、これは で達成されheightForRowAtIndexPath:ます。コメントに記載されているように、size.heightセル全体ではなくテキスト ビューの高さのみであるため、オフセットを追加する必要があります。この例の場合、この値は 80 でした。

于 2013-10-21T03:20:39.390 に答える
3

自動レイアウトを使用している場合の 1 つの方法は、自動レイアウト エンジンにサイズを計算させることです。これは最も効率的な方法ではありませんが、非常に便利です (そして間違いなく最も正確です)。セル レイアウトが複雑になるにつれて、より便利になります。たとえば、セルに 2 つ以上のテキストビュー/フィールドが突然存在する場合などです。

自動レイアウトを使用してテーブルビュー セルのサイズを変更するための完全なサンプルを使用して、同様の質問に答えました。

自動レイアウトですべてのサブビューに合わせてスーパービューのサイズを変更する方法は?

于 2013-09-16T15:51:03.600 に答える
1

完全なスムーズなソリューションは次のとおりです。

まず、textView を持つ cell クラスが必要です

@protocol TextInputTableViewCellDelegate <NSObject>
@optional
- (void)textInputTableViewCellTextWillChange:(TextInputTableViewCell *)cell;
- (void)textInputTableViewCellTextDidChange:(TextInputTableViewCell *)cell;
@end

@interface TextInputTableViewCell : UITableViewCell
@property (nonatomic, weak) id<TextInputTableViewCellDelegate> delegate;
@property (nonatomic, readonly) UITextView *textView;
@property (nonatomic) NSInteger minLines;
@property (nonatomic) CGFloat lastRelativeFrameOriginY;
@end


#import "TextInputTableViewCell.h"

@interface TextInputTableViewCell () <UITextViewDelegate> {
    NSLayoutConstraint *_heightConstraint;
}
@property (nonatomic) UITextView *textView;
@end

@implementation TextInputTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        _textView = [UITextView new];
        _textView.translatesAutoresizingMaskIntoConstraints = NO;
        _textView.delegate = self;
        _textView.scrollEnabled = NO;
        _textView.font = CELL_REG_FONT;
        _textView.textContainer.lineFragmentPadding = 0.0;
        _textView.textContainerInset = UIEdgeInsetsZero;
        [self.contentView addSubview:_textView];

        [self.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[view]-|" options:nil metrics:nil views:@{@"view": _textView}]];
        [self.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[view]-|" options:nil metrics:nil views:@{@"view": _textView}]];

        _heightConstraint = [NSLayoutConstraint constraintWithItem: _textView
                         attribute: NSLayoutAttributeHeight
                         relatedBy: NSLayoutRelationGreaterThanOrEqual
                         toItem: nil
                         attribute: NSLayoutAttributeNotAnAttribute
                         multiplier: 0.0
                         constant: (_textView.font.lineHeight + 15)];
        _heightConstraint.priority = UILayoutPriorityRequired - 1;
        [_textView addConstraint:_heightConstraint];
    }
    return self;
}

- (void)prepareForReuse {
    [super prepareForReuse];    
    self.minLines = 1;
}

- (void)setMinLines:(NSInteger)minLines {
    _heightConstraint.constant = minLines * _textView.font.lineHeight + 15;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([self.delegate respondsToSelector:@selector(textInputTableViewCellTextWillChange:)]) {
        [self.delegate textInputTableViewCellTextWillChange:self];
    }
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView {
    if ([self.delegate respondsToSelector:@selector(textInputTableViewCellTextDidChange:)]) {
        [self.delegate textInputTableViewCellTextDidChange:self];
    }
}

次に、TableViewController で使用します

@interface SomeTableViewController () <TextInputTableViewCellDelegate>
@end

@implementation SomeTableViewController

. . . . . . . . . . . . . . . . . . . .

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    TextInputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TextInputTableViewCellIdentifier forIndexPath:indexPath];
    cell.delegate = self;
    cell.minLines = 3;
    . . . . . . . . . .  
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (void)textInputTableViewCellWillChange:(TextInputTableViewCell *)cell {
    cell.lastRelativeFrameOriginY = cell.frame.origin.y - self.tableView.contentOffset.y;
}

- (void)textInputTableViewCellTextDidChange:(TextInputTableViewCell *)cell {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    [UIView performWithoutAnimation:^{
        [self.tableView moveRowAtIndexPath:indexPath toIndexPath:indexPath];
    }];

    CGFloat contentOffsetY = cell.frame.origin.y - cell.lastRelativeFrameOriginY;
    self.tableView.contentOffset = CGPointMake(self.tableView.contentOffset.x, contentOffsetY);

    CGRect caretRect = [cell.textView caretRectForPosition:cell.textView.selectedTextRange.start];
    caretRect = [self.tableView convertRect:caretRect fromView:cell.textView];

    CGRect visibleRect = self.tableView.bounds;
    visibleRect.origin.y += self.tableView.contentInset.top;
    visibleRect.size.height -= self.tableView.contentInset.top + self.tableView.contentInset.bottom;
    BOOL res = CGRectContainsRect(visibleRect, caretRect);
    if (!res) {
        caretRect.size.height += 5;
        [self.tableView scrollRectToVisible:caretRect animated:NO];
    }
}
@end
  • ここでminLinesは、textView の最小の高さを設定できます (UITableViewAutomaticDimension を使用した AutoLayout による高さの最小化に抵抗するため)。

  • moveRowAtIndexPath:indexPath:同じ indexPath を使用すると、tableViewCell の高さの再計算と再レイアウトが開始されます。

  • performWithoutAnimation:副作用を取り除きます (入力中に新しい行を開始するときに tableView コンテンツ オフセットがジャンプします)。

  • 現在のセルが autoLayout 計算によって予期しない方法で変更される可能性があるため、セルの更新中に保持するrelativeFrameOriginY(! ではない )ことが重要です。長い単語を入力しているときに、システムのハイフネーションの視覚的なジャンプを取り除きます。contentOffsetYcontentSize

  • プロパティを設定しないでください estimatedRowHeight以下は動作しません

    self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;
    

    tableViewDelegate メソッドのみを使用してください。

================================================== ========================

tableViewtableViewCellの間の弱いバインディングと tableViewCell からのtableViewのジオメトリの更新を気にしない場合は、TextInputTableViewCell上記のクラスをアップグレードすることができます。

@interface TextInputTableViewCell : UITableViewCell
@property (nonatomic, weak) id<TextInputTableViewCellDelegate> delegate;
@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, readonly) UITextView *textView;
@property (nonatomic) NSInteger minLines;
@end


#import "TextInputTableViewCell.h"

@interface TextInputTableViewCell () <UITextViewDelegate> {
    NSLayoutConstraint *_heightConstraint;
    CGFloat _lastRelativeFrameOriginY;
}
@property (nonatomic) UITextView *textView;
@end

@implementation TextInputTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        _textView = [UITextView new];
        _textView.translatesAutoresizingMaskIntoConstraints = NO;
        _textView.delegate = self;
        _textView.scrollEnabled = NO;
        _textView.font = CELL_REG_FONT;
        _textView.textContainer.lineFragmentPadding = 0.0;
        _textView.textContainerInset = UIEdgeInsetsZero;
        [self.contentView addSubview:_textView];

        [self.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[view]-|" options:nil metrics:nil views:@{@"view": _textView}]];
        [self.contentView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[view]-|" options:nil metrics:nil views:@{@"view": _textView}]];

        _heightConstraint = [NSLayoutConstraint constraintWithItem: _textView
                         attribute: NSLayoutAttributeHeight
                         relatedBy: NSLayoutRelationGreaterThanOrEqual
                         toItem: nil
                         attribute: NSLayoutAttributeNotAnAttribute
                         multiplier: 0.0
                         constant: (_textView.font.lineHeight + 15)];
        _heightConstraint.priority = UILayoutPriorityRequired - 1;
        [_textView addConstraint:_heightConstraint];
    }
    return self;
}

- (void)prepareForReuse {
    [super prepareForReuse];    
    self.minLines = 1;
    self.tableView = nil;
}

- (void)setMinLines:(NSInteger)minLines {
    _heightConstraint.constant = minLines * _textView.font.lineHeight + 15;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    _lastRelativeFrameOriginY = self.frame.origin.y - self.tableView.contentOffset.y;
    return YES;
}

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

    NSIndexPath *indexPath = [self.tableView indexPathForCell:self];
    if (indexPath == nil) return;

    [UIView performWithoutAnimation:^{
        [self.tableView moveRowAtIndexPath:indexPath toIndexPath:indexPath];
    }];

    CGFloat contentOffsetY = self.frame.origin.y - _lastRelativeFrameOriginY;
    self.tableView.contentOffset = CGPointMake(self.tableView.contentOffset.x, contentOffsetY);

    CGRect caretRect = [self.textView caretRectForPosition:self.textView.selectedTextRange.start];
    caretRect = [self.tableView convertRect:caretRect fromView:self.textView];

    CGRect visibleRect = self.tableView.bounds;
    visibleRect.origin.y += self.tableView.contentInset.top;
    visibleRect.size.height -= self.tableView.contentInset.top + self.tableView.contentInset.bottom;

    BOOL res = CGRectContainsRect(visibleRect, caretRect);
    if (!res) {
        caretRect.size.height += 5;
        [self.tableView scrollRectToVisible:caretRect animated:NO];
    }
}
@end
于 2016-06-04T18:49:35.887 に答える
0

迅速なバージョン

func textViewHeightForAttributedText(text: NSAttributedString, andWidth width: CGFloat) -> CGFloat {
    let calculationView = UITextView()
    calculationView.attributedText = text
    let size = calculationView.sizeThatFits(CGSize(width: width, height: CGFloat.max))
    return size.height
}
于 2016-04-25T09:33:42.060 に答える
0

UITableViewCellインナーの高さを基準に の高さを自動調整したい場合UITextView。ここで私の答えを参照してください: https://stackoverflow.com/a/45890087/1245231

ソリューションは非常にシンプルで、iOS 7 以降で機能するはずです。StoryBoard内のScrolling Enabledオプションがオフになっていることを確認してください。UITextViewUITableViewCell

次に、UITableViewController の viewDidLoad() で次のように設定tableView.rowHeight = UITableViewAutomaticDimensionします。tableView.estimatedRowHeight > 0

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 44.0
}

それでおしまい。UITableViewCellの高さは、インナーの高さに基づいて自動的に調整されUITextViewます。

于 2017-08-28T12:02:49.610 に答える