0

カスタムセルでUITableViewを実装しましたが、

私がやりたいことは、UITableViewCellテキストの長さに応じて高さを変更したいです(動的な高さ)

これが私のコードスニペットです。

#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 320.0f
#define CELL_CONTENT_MARGIN 10.0f


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    NSString *text = [DescArr objectAtIndex:[indexPath row]];
    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
    CGFloat height = MAX(size.height, 55.0);

    return height;
}

の高UITableViewCellさはちゃんと変わるのにCustomCellの高さが変わらず、

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"BTSTicketsCellIdentifier";
    CRIndCoupnCell *cell = (CRIndCoupnCell *)[tblDescriptn dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CRIndCoupnCell" owner:self options:nil];
        for (id oneObject in nib) if ([oneObject isKindOfClass:[CRIndCoupnCell class]])
            cell = (CRIndCoupnCell *)oneObject;
    }

    NSLog(@"cell.frame.size.height=%f",cell.frame.size.height);

    cell.lblleft.text=[arr objectAtIndex:indexPath.row];
    cell.lblRight.text=[DescArr objectAtIndex:indexPath.row];

    return cell;
}

マイ ログはcell.frame.size.height=100.0すべての行に表示されます。CustomCell の高さは変わりません。

: どこで間違いを犯していますか? 助けてください。

前もって感謝します

4

1 に答える 1

1

セルの全幅からセル マージンの 2 倍を差し引いたサイズと最大高さ 2000 に文字列を制限していますが、文字列を緑色のテキスト色のラベルのテキストとして設定しているため、サイズを確認する必要があります。そのラベルの幅と 2000 または任意の任意の高さに応じて。

CGSize constraint = CGSizeMake(MYGREENLABEL_WIDTH, 2000);
于 2013-08-30T10:52:38.423 に答える