22

UITableView行の高さを取得してUITableViewCellのサイズに自動サイズ変更するにはどうすればよいですか?

したがって、Interface BuilderでUITableViewCellを作成していて、その高さが標準サイズよりも高いと仮定すると、UITableView行の高さを取得してそれに合わせて自動サイズ設定するにはどうすればよいですか?(つまり、Interface Builderで高さを手動で測定する必要があり、プログラムで設定するのとは対照的です)

4

5 に答える 5

37

http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/は、これに関する優れたチュートリアルです。

メインビットは

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
  // Get the text so we can measure it
  NSString *text = [items objectAtIndex:[indexPath row]];
  // Get a CGSize for the width and, effectively, unlimited height
  CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
  // Get the size of the text given the CGSize we just made as a constraint
  CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
  // Get the height of our measurement, with a minimum of 44 (standard cell size)
  CGFloat height = MAX(size.height, 44.0f);
  // return the height, with a bit of extra padding in
  return height + (CELL_CONTENT_MARGIN * 2);
}
于 2011-11-15T18:59:26.960 に答える
32

すべてのセルが同じである場合は、rowHeightプロパティをUITableViewセルのサイズに設定します。コンテンツに基づいてすべてが異なる場合は-tableView:heightForRowAtIndexPath:、データソースに基づいて各行の高さを実装および計算する必要があります。

于 2011-03-01T23:45:33.747 に答える
13

テーブルビューを含む xib で、セル オブジェクトを追加し、それをソース コードの IBOutlet にリンクできます。どこでも使用しません。これを使用して、セルの高さを取得します。

次に、tableView:heightForRowAtIndexPath:そのオブジェクトを使用して高さを取得できます。100% 自動ではありませんが、少なくともこれにより、IB のセル ビューを変更するときにソース コードを手動で更新する手間が省けます。


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return myDummyCellObject.bounds.size.height;
}

すべての行が同じタイプ (セル) の場合、tableView.rowHeight上記のデリゲート メソッドを実装する代わりに、プログラムでプロパティを設定できます。シナリオによって異なります。

ああ、myDummyCellObject を で解放することを忘れないでください-dealloc

于 2011-03-02T00:08:37.747 に答える
0
self.tableView.estimatedRowHeight = 65.0; // Estimate the height you want
self.tableView.rowHeight = UITableViewAutomaticDimension; // auto change heights for multiple lines cells.

UITableViewDataSource 必須メソッドを実装する場合:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = [self.todoArray objectAtIndex:indexPath.row];
    cell.textLabel.numberOfLines = 0; // allow multiple lines showing up

    return cell;
}
于 2016-07-03T21:55:49.980 に答える
0

iOS 8 以降、テーブル ビューで次のオプションを指定することにより、セルフ サイズのセルを操作するオプションがあります。

tableView.estimatedRowHeight = 85.0
tableView.rowHeight = UITableView.automaticDimension

これは、システムが既存の制約またはコンテンツに基づいて行を計算できる限り機能します。automaticDimension を設定すると、heightForRowAtIndexPath が呼び出されないことに注意してください。


より複雑なデータでは、自動的に計算できるセルもあれば、特定の高さ計算ロジックが必要なセルもあります。この場合、estimatedRowHeightのみを設定してから、正しく機能するセルの自動ロジックを使用して cellForRowAtIndexPath を実装する必要があります。

// Set the estimated row height in viewDidLoad, but *not* automatic dimension!
override func viewDidLoad() {
    // other viewDidLoad stuff...
    tableView.estimatedRowHeight = 85.0
    tableView.delegate = self
}

// Then implement heightForRowAtIndexPath delegate method
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if theAutomaticCalculationWorksForThisRow {
        return UITableView.automaticDimension
    } else {
        // Calculate row height based on custom logic...
        let rowHeight = 85.0
        return rowHeight
    }
}
于 2018-12-14T12:07:01.010 に答える