关于iPhone:更改UITableView自定义单元格的高度

Change Height of UITableView Customcell

我已经使用自定义单元

实现了UITableView

我想做的是,我想根据文本长度(动态高度)

更改高度UITableViewCell

这是我的代码段,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#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的高度未更改,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- (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的高度不变。

>:<br />
我在哪里犯错?请帮忙。</p>
<p>预先感谢</p>
<div class=


您正在将字符串限制为单元格全宽的大小减去单元格边距的2倍,最大高度为2000,但是由于您将字符串设置为带有绿色文本颜色的标签文本,因此您应该检查一下根据标签宽度和任意高度2000的大小或您选择的任何值。

1
CGSize constraint = CGSizeMake(MYGREENLABEL_WIDTH, 2000);