ゴナはここに自分の質問に答える:
もともと私は高さの計算がtableView:cellForRowAtIndexPath:
法に縛られたことをかなり確信していた、と他の場所に移動することができませんでした。しかし、たくさんのリストラをして、私はそこから、そしてすべてを解決するtableView:heightForRowAtIndexPath:
にそのものを取り込めました。
自動高さ調整細胞が仕事を得るためにしようとしている他の誰のために、ここに役立つかもしれないいくつかのコードがあります:
// Inside tableView:cellForRowAtIndexPath:
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = self.numberOfTextRows;
// numberOfTextRows is an integer, declared in the class
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGSize theSize = [theObject.theStringToDisplay sizeWithFont:[UIFont systemFontOfSize:18.0f] constrainedToSize:CGSizeMake(265.0f, 9999.0f) lineBreakMode:UILineBreakModeWordWrap];
// This gets the size of the rectangle needed to draw a multi-line string
self.numberOfTextRows = round(theSize.height/18);
// 18 is the size of the font used in the text label
// This will give us the number of lines in the multi-line string
if ((indexPath.section == FIXED_HEIGHT_SECTION) || (self.numberOfTextRows < 2)) {
return 44;
// 44 is the default row height; use it for empty or one-line cells (or in other table sections)
} else {
return theSize.height + 16;
// 16 seems to provide a decent space above/below; tweak to taste
}
}
あなたは適切なセルの高さを計算するためのより正確な方法を考えることができれば、私はすべての耳です。 :)
を以下のよう
tableView:heightForRowAtIndexPath:
の実装内部tableView:cellForRowAtIndexPath:
を呼び出すことでしたあなたは矩形の高さを18で割り、それを再び掛けますが、それ以外は同じようにします。 –さて、最初の部門は、必要な行の数を調べることで、 'cell.textLabel.numberOfLines'を正しく設定できます。それを設定しなければ、すべてが1行に印刷されます。しかし、ええ、私はちょうど 'self.numberOfTextRows'の代わりに最後のビットで' theSize.height'を使うことができたと思います。 – Triz
明快さと簡略化のために答えを編集しました。本当にうまくいきます。 – Triz