画像、タイトル、説明を持つ UITableViewCell サブクラスがあります。説明の内容の長さに応じてセルの高さのサイズを変更することになっています。つまり、セルが 5 行を超える場合は、それが続くまで拡張する必要があります (+ 画像などの他のサブビュー)。
次に来るセルは、その後にのみ開始する必要があります。
固定行の高さ = 160 を持つ xib からインスタンス化された UITableViewCell サブクラスがあります。
これはかなり標準的な要件であることはわかっていますが、ガイドラインが見つかりません。
私はすでに次のようにlayoutSubViewsを拡張しました:
- (void) layoutSubviews
{
[self resizeCellImage];
}
- (void) resizeCellImage
{
CGRect descriptionRect = self.cellDescriptionLabel.frame;
CGRect imageRect = self.cellImageView.frame;
float descriptionBottomEdgeY = descriptionRect.origin.y + descriptionRect.size.height;
float imageBottomEdgeY = imageRect.origin.y + imageRect.size.height;
if (imageBottomEdgeY >= descriptionBottomEdgeY)
return;
//push the bottom of image to the bottom of description
imageBottomEdgeY = descriptionBottomEdgeY;
float newImageHeight = imageBottomEdgeY - imageRect.origin.y;
imageRect.size.height = newImageHeight;
self.cellImageView.frame = imageRect;
CGRect cellFrame = self.frame;
cellFrame.size.height = imageRect.size.height + imageRect.origin.y + 5;
CGRect contentFrame = self.contentView.frame;
contentFrame.size.height = cellFrame.size.height - 1;
self.contentView.frame = contentFrame;
self.frame = cellFrame;
}
説明が画像よりも高い場合は、画像とセルの高さを説明に合わせてサイズ変更する必要があることがわかります。
ただし、これを実行してこのコードを呼び出すと、次のようになります。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.cellDescriptionLabel.text = @"Some long string";
[cell.cellDescriptionLabel sizeToFit];
[cell setNeedsLayout];
return cell;
}
呼び出しによりセル フレームが変更されている間layoutSubViews、他のセルはそれを尊重していないようです。つまり、前のセルのサイズが変更されていなければ、同じ位置に表示されます。
2 つの質問:
- 私が望むものを可能にする方法は?
setNeedsLayout内で呼び出すことで、私は正しくやっていますcellForRowAtIndexPathか?
PS: セルの高さを変更する鍵を握っていることは知っていますが、高さを計算するためだけにheightForRowAtIndexPath行うデータ解析 (ここには示されていません) はやり過ぎだと感じています。コンテンツのニーズに応じてサイズを変更するように にcellForRowAtIndexPath直接指示できるものが必要です。UITableViewCell