1

セルに UILabel があり、UILabel の高さを動的に変更する必要があります。したがって、セルは、高さ 44、ラベル 240x44、および水平方向と垂直方向の自動サイズ調整を持つ xib ファイルです。フォントは15.0fサイズ、システムです。行数 = 0、単語単位で折り返す。

だから私は方法を持っています

+ (CGFloat) cellHeightForString: (NSString *) string
{
    CGFloat cellHeight = [string sizeWithFont:[UIFont systemFontOfSize:15.0f] constrainedToSize:CGSizeMake(240.0f, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping].height;
    return  cellHeight < 44.0f ? 44.0f : cellHeight;
}

ただし、セルの高さが小さい場合があるため、テキストのすべての行がセルに表示されるわけではありません。私が間違っていることを理解できません..何か助けはありますか?

4

1 に答える 1

0

これを試して、

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// add your code snippet here
// dynamically change the label height,
       CGSize textSize = {
        200.0,   // limit width
        20000.0  // and height of text area
    };

    CGSize contentSize = [yourText sizeWithFont:[UIFont systemFontOfSize:15.0] constrainedToSize:textSize lineBreakMode:NSLineBreakByWordWrapping];
    CGFloat contentHeight = contentSize.height < 36? 36: contentSize.height; // lower bound for height

    CGRect labelFrame = [yourLabel frame];
    yourLabel.size.height = contentHeight;
    [yourLabel setFrame: labelFrame];
    [yourLabel setText:yourText];
    return cell;
}

セルの高さを動的に変更し、

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YourCell *currentCell = (YourCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath];

// change hard coded value based on your cell alignment

    int cellLength=[currentCell.yourLabel.text length]/42;
    cellLength = cellLength*22 > 200 ? cellLength*22 : 200;
    CGSize constraint = CGSizeMake((200 - 10), cellLength);
    CGSize size1 = [cellText sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];
    return 220+size1.height;
}
于 2013-11-07T12:04:43.353 に答える