私は上記の問題でいくつかの問題を抱えています。テーブル ビュー (X-300、Y-26、幅 192、高さ 42) に、さまざまな長さのランダムな未知の文字列を含むラベルがあります。行の最大数は 2 にする必要があります。テキストは常にラベルの上部にある必要があります。
私は実用的な解決策(以下)を持っていますが、それはとても汚いようです-とてもシンプルに見える何かを行うためのよりクリーンな方法が必要です:
UILabel *cellLabel = (UILabel *)[cell viewWithTag:2];
// First set cell lines back to 0 and reset height and width of the label - otherwise it works until you scroll down as cells are reused.
cellLabel.numberOfLines = 0;
cellLabel.frame = CGRectMake(cellLabel.frame.origin.x, cellLabel.frame.origin.y, 192, 42);
// Set the text and call size to fit
[cellLabel setText:[[products objectAtIndex:indexPath.row] objectForKey:@"title"]];
[cellLabel sizeToFit];
// Set label back to 2 lines.
cellLabel.numberOfLines = 2;
// This 'if' solves a weird the problem when the text is so long the label ends "..." - and the label is slightly higher.
if (cellLabel.frame.size.height > 42) {
cellLabel.frame = CGRectMake(cellLabel.frame.origin.x, cellLabel.frame.origin.y, 192, 42);
}