0

コンテンツの量に基づいて UITableViewCells をスケーリングするにはどうすればよいですか? 私のセルでは、フォーラムを表す 3 つのラベルを使用しています。ラベルには「エイリアス」、「日付」、「コメント」という名前が付いています。3 番目のラベル コメントは、任意の数の行にすることができます。したがって、「コメント」ラベルにあるテキスト(サイズ)の量に応じて、セルを動的にする必要があります。以下は私の関連コードです:

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

if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

Feedback *item = [self.items objectAtIndex:indexPath.row];

UILabel *aliasLabel = (UILabel *)[cell viewWithTag:1];
UILabel *commentLabel = (UILabel *)[cell viewWithTag:2];
UILabel *dateLabel = (UILabel *)[cell viewWithTag:3];

[aliasLabel setText:item.alias];
[commentLabel setText:item.comment];
[dateLabel setText:[self.dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:(double)item.time]]];

commentLabel.numberOfLines = 0;
[commentLabel sizeToFit];

return cell;
}

このメソッドでは、commentLabel の高さに応じて tableViewCell の高さを設定することになっています。唯一の問題は、その方法がわからないことです。

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

    return ????;
    //return [indexPath row] + 50;

}

return ????;
}

私のコードを見て、これを機能させる方法の例を挙げようとする人の助けが必要です。結局のところ、そのラベルは 1 つだけです....私はすでにいくつかのチュートリアルを見てきましたが、それを機能させることができませんでした...あなたの時間と助けに感謝します. これは古いコードを含む古い画面です。これは多かれ少なかれ現在のフォーラムの様子です: http://tinypic.com/view.php?pic=16h7me9&s=6

4

1 に答える 1

0

必要なのは、特定のフォントでレンダリングされたテキストのサイズを計算する方法です。幸いなことに、UIKit はNSString UIKit AdditionssizeWithFont:のさまざまなメソッドを介して、まさにこれを提供します。

于 2012-10-29T13:24:06.907 に答える