セル内のコンテンツに基づいてテーブル ビュー セルの高さを調整しようとしています。
セルの高さが違うので動いているようですが、文字がうまく表示されず困っています。
テーブル ビューとプロトタイプ セルはストーリーボードで作成され、プロトタイプ セルにリンクされたカスタム セル クラスがあります。私のプロトタイプ セルには、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 以降では動作しません。
だから、誰かがこれを手伝ってくれて、私が間違っていることを理解するのを手伝ってくれることを願っています.