1

セル内のコンテンツに基づいてテーブル ビュー セルの高さを調整しようとしています。

セルの高さが違うので動いているようですが、文字がうまく表示されず困っています。

テーブル ビューとプロトタイプ セルはストーリーボードで作成され、プロトタイプ セルにリンクされたカスタム セル クラスがあります。私のプロトタイプ セルには、3 つのラベルがあります。タイトル ラベル、日付ラベル、動的テキスト データを含むラベル。ダイナミック テキスト データ ラベルの行番号は 0 に設定され、改行はストーリーボードの属性インスペクターで「ワード ラッピング」に設定されます。

アプリを実行すると、セルの高さがすべて異なるため、インデックス パスの行の高さが機能すると仮定します。

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Text data
    NSDictionary *cellOutputDict = [newsDataArray objectAtIndex:indexPath.row];
    NSString *text = [NSString stringWithFormat:@"%@", [cellOutputDict objectForKey:@"text"]];

    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text attributes:nil];

    CGFloat width = 280;
    CGRect rect = [attributedText boundingRectWithSize:(CGSize){width, CGFLOAT_MAX}
                                               options:NSStringDrawingUsesLineFragmentOrigin
                                               context:nil];
    CGSize size = rect.size;

    return size.height + 2 * 20; // size.height plus 2 times the desired margin
}

そして、これはインデックスパスの行の私のセルです:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"NewsCell";
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // If the cell doesn't exists create it
    if (cell == nil) {
        cell = [[NewsCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...

    // Text data
    NSDictionary *cellOutputDict = [newsDataArray objectAtIndex:indexPath.row];

    cell.titleLabel.text = [NSString stringWithFormat:@"%@", [cellOutputDict objectForKey:@"title"]];
    cell.dateLabel.text = [NSString stringWithFormat:@"%@", [cellOutputDict objectForKey:@"date"]];
    cell.newsTextLabel.text = [NSString stringWithFormat:@"%@", [cellOutputDict objectForKey:@"text"]];

    return cell;
}

動的テキスト データを出力する cell.newsTextLabel.text がセルに表示されますが、最初の行のみです。

iOS 6.1 で動的なセルの高さを表示する例をいくつか試してみましたが、問題なく動作しますが、iOS 7 以降では動作しません。

だから、誰かがこれを手伝ってくれて、私が間違っていることを理解するのを手伝ってくれることを願っています.

ここに画像の説明を入力

4

4 に答える 4

1

これを試して

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

    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    float height = cell.frame.size.height;
    return height;
}
于 2013-12-06T15:09:39.167 に答える
1

ラベルをセルに合わせて拡張するには、ラベルを上下のビューに結び付ける制約を与える必要があります。この場合、それはセルの下部に対する制約のように見え、ラベルに対する制約のように見えます ("Titel. ..") その上にあります。

于 2013-12-06T17:21:12.693 に答える
1

行の高さと推定行の高さの自動寸法を設定するには、次の手順を実行して、セル/行の高さのレイアウトに自動寸法を有効にします。

  • tableview dataSource とデリゲートの割り当てと実装
  • UITableViewAutomaticDimensionrowHeight と EstimatedRowHeight に割り当てます
  • デリゲート/データソース メソッドを実装する (つまりheightForRowAt、それに値を返すUITableViewAutomaticDimension)

-

目標 C:

// in ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

  @property IBOutlet UITableView * table;

@end

// in ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    self.table.dataSource = self;
    self.table.delegate = self;

    self.table.rowHeight = UITableViewAutomaticDimension;
    self.table.estimatedRowHeight = UITableViewAutomaticDimension;
}

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

    return UITableViewAutomaticDimension;
}

迅速:

@IBOutlet weak var table: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Don't forget to set dataSource and delegate for table
    table.dataSource = self
    table.delegate = self

    // Set automatic dimensions for row height
    // Swift 4.2 onwards
    table.rowHeight = UITableView.automaticDimension
    table.estimatedRowHeight = UITableView.automaticDimension


    // Swift 4.1 and below
    table.rowHeight = UITableViewAutomaticDimension
    table.estimatedRowHeight = UITableViewAutomaticDimension

}



// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    // Swift 4.2 onwards
    return UITableView.automaticDimension

    // Swift 4.1 and below
    return UITableViewAutomaticDimension
}

UITableviewCell のラベル インスタンスの場合

  • 行数を 0 に設定 (& 改行モード = 末尾を切り捨て)
  • そのスーパービュー/セル コンテナーに関して、すべての制約 (上、下、右左) を設定します。
  • オプション: データがなくても、ラベルで覆われる最小の垂直領域が必要な場合は、ラベルの最小の高さを設定します。

ここに画像の説明を入力

: 動的な長さのラベル (UIElements) が複数ある場合は、コンテンツのサイズに応じて調整する必要があります: 優先度の高い展開/圧縮を行うラベルの [コンテンツのハグと圧縮抵抗の優先度] を調整します。

于 2017-10-17T09:13:31.270 に答える
0
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByClipping;
于 2013-12-06T18:48:27.490 に答える