0

テーブル セルで UILabel の高さを変更しようとしています。私の場合、テーブルは動的で、2 つの UILabel を使用してカスタマイズしました。Table にはちょうど 11 項目が表示されますが、セルのうち 2 つは複数行のテキスト UILabel に対応する必要があります。次のように、2 つのセルの高さを選択的に増やしました。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 3 || indexPath.row == 10)
         return 100;
    else
        return 46;
}

しかし、これら 2 つのセルだけの UILabel の高さを増やす必要もあります。次のコードを使用してみました (このサイトの他の例も試しました)。

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


    // Configure the cell...
    UILabel *DetailLabel = (UILabel *)[cell.contentView viewWithTag:10];
    UILabel *DetailText = (UILabel *)[cell.contentView viewWithTag:20];

    if(indexPath.row == 3 || indexPath.row ==10){
        [DetailText setText:[self.EmployerViewText objectAtIndex:[indexPath row]]];

        DetailText.lineBreakMode = NSLineBreakByWordWrapping;
        DetailText.font = [UIFont systemFontOfSize:17.0f];
        DetailText.numberOfLines = 0;
        DetailText.frame = CGRectMake(0, 0, 317, 80);
        [DetailText sizeToFit];

    }else{

        [DetailText setText:[self.EmployerViewText objectAtIndex:[indexPath row]]];
    }

    [DetailLabel setText:[self.EmployerViewLabel objectAtIndex:[indexPath row]]];

   return cell;
}

残念ながら、UILable のサイズは変更されていません。誰かが私が間違っていることを指摘できますか?

4

2 に答える 2

1

問題は、DetailText のテキストを変更するときに、[DetailText sizeToFit] を再度呼び出さないことです。

    DetailText = [[UILabel alloc]initWithFrame:CGRectMake(60, 200, 100, 30)];
    DetailText.backgroundColor = [UIColor orangeColor];

    DetailText.lineBreakMode = NSLineBreakByWordWrapping;
    DetailText.font = [UIFont systemFontOfSize:17.0f];
    DetailText.numberOfLines = 0; // Here is the key
    DetailText.text = @"test message test message test message test message test message test message";
    DetailText.frame = CGRectMake(100, 0, 100, 80);
    [DetailText sizeToFit];

テキストを変更した後:

    DetailText.text = @"this is the modified text";
    [DetailText sizeToFit];

このコードを試してみたところ、サイズが変更されました。

于 2013-10-19T10:31:16.547 に答える
0

UILabel.h から:

// this determines the number of lines to draw and what to do when sizeToFit is called. default value is 1 (single line). A value of 0 means no limit
// if the height of the text reaches the # of lines or the height of the view is less than the # of lines allowed, the text will be
// truncated using the line break mode.

@property(nonatomic) NSInteger numberOfLines;

複数行の UILabel を許可するだけです:

    ...
    [DetailText setText:[self.EmployerViewText objectAtIndex:[indexPath row]]];

    DetailText.lineBreakMode = NSLineBreakByWordWrapping;
    DetailText.font = [UIFont systemFontOfSize:17.0f];
    DetailText.numberOfLines = 0; // Here is the key

    DetailText.frame = CGRectMake(0, 0, 317, 80);
    [DetailText sizeToFit];
    ...

他の場合には、numberOfLines を 1 に設定することをお勧めします。

于 2013-10-19T10:09:35.010 に答える