簡體   English   中英

更改UITableView Customcell的高度

[英]Change Height of UITableView Customcell

我已經使用自定義單元實現了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的高度不變。

:我在哪里犯錯? 請幫忙。

提前致謝

您正在將字符串限制為單元格全寬的大小減去單元格邊距的2倍,最大高度為2000,但是由於您將字符串設置為帶有綠色文本顏色的標簽文本,因此您應該檢查大小根據標簽的寬度和任意高度2000或您選擇的任何值。

CGSize constraint = CGSizeMake(MYGREENLABEL_WIDTH, 2000);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM