4

ユーザーからのコメントであるカスタム UITableViewCells を持つ UITableView があります。現在、セルの高さは 115.0f ですが、コメントの長さに基づいて高さを変更したいと考えています。コメントが 3 行を超える場合、ユーザーがセルを選択できるようにし、セルを展開してコメント全体を表示できるようにします。メソッドを使用し[UIView animateWithDuration: completion:てセルを拡張していますが、テキストの長さに基づいてセルの正しいサイズを特定する方法がわかりません。誰かが私を助けることができますか?ここにいくつかのコードがあります:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    if (indexPath.row == 0)
    {
        return 480;
    }
    else
    {
        if (self.cellType == 0)
        {   
            return 115;
        }
        else
        {
            return 75;
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row > 0)
    {
        NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
        if ([cell isKindOfClass:[CommentCell class]])
        {
            CommentCell *cell = (CommentCell *)[tableView cellForRowAtIndexPath:indexPath];
            UILabel *label = cell.commentBodyLabel;
            NSString *theText = label.text;
            CGSize constraintSize = CGSizeMake(label.frame.size.width, label.frame.size.height);
            CGSize labelSize = [theText sizeWithFont:label.font constrainedToSize:constraintSize lineBreakMode:label.lineBreakMode];
            CGFloat labelHeight = labelSize.height;
            int numLines = (int)(labelHeight/label.font.leading);
            NSLog(@"there are %i lines", numLines);
            NSLog(@"The text height is %f", labelHeight);
            if (numLines == 3)
            {
                //This is where I should expand the cell
            }
        }
4

2 に答える 2

1

1 つの方法は、プロトタイプ セルをインスタンス化し、与えられたデータに基づいて動的な高さを計算させることです。このメソッドを使用すると、ビュー コントローラーでセルの構成に関する知識をハードコーディングする必要なく、ストーリーボード レイアウト (ストーリーボードを使用している場合) に依存できます。

編集TLIndexPathToolsを使用して動的な高さラベルを持つセルの実際の例を追加しました。これは、セルがTLDynamicHeightViewプロトコルを実装している場合に動的な高さを自動的に計算します。「動的な高さ」のサンプル プロジェクトを参照してください。高さの計算は、次のようにセルで行われます。

@interface DynamicHeightCell ()
@property (nonatomic) CGSize originalSize;
@property (nonatomic) CGSize originalLabelSize;
@end

@implementation DynamicHeightCell

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.originalSize = self.bounds.size;
    self.originalLabelSize = self.label.bounds.size;
}

- (void)configureWithText:(NSString *)text
{
    self.label.text = text;
    [self.label sizeToFit];
}

#pragma mark - TLDynamicSizeView

- (CGSize)sizeWithData:(id)data
{
    [self configureWithText:data];
    CGSize labelSize = self.label.bounds.size;
    CGSize size = self.originalSize;
    size.width += labelSize.width - self.originalLabelSize.width;
    size.height += labelSize.height - self.originalLabelSize.height;
    return size;
}

@end

基本的に、セルがストーリーボードから目覚めたときの元のサイズを覚えています。次に、計算が実行sizeToFitされたら、ラベルを呼び出し、新しいサイズを使用して高さのデルタを計算し、それを元の高さに追加します。ストーリーボードでは、ラベルの幅を目的の幅に設定し、行数を 0 に設定する必要があります。

ビュー コントローラー側で を使用している場合はTLIndexPathTools、文字列の配列を使用してデータ モデルを設定し、セルにラベルを設定するだけです。プロトタイプとサイズの計算は、基本TLTableViewControllerクラスによって自動的に行われます。を使用したくない場合はTLIndexPathTools、 からビットを引っ張るTLTableViewControllerとうまくいくはずです。

@implementation DynamicHeightTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.indexPathController.items = @[
          @"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
          @"Fusce ac erat at lectus pulvinar porttitor vitae in mauris. Nam non eleifend tortor.",
          @"Quisque tincidunt rhoncus pellentesque.",
          @"Duis mauris nunc, fringilla nec elementum nec, lacinia at turpis. Duis mauris nunc, fringilla nec elementum nec, lacinia at turpis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.",
          ];
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    NSString *item = [self.indexPathController.dataModel itemAtIndexPath:indexPath];
    DynamicHeightCell *dynamicCell = (DynamicHeightCell *)cell;
    [dynamicCell configureWithText:item];
}
于 2013-05-31T19:29:43.983 に答える