2013-08-30 85 views
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的高度不会改变。

: 我在哪里犯错?请帮忙。

预先感谢

+0

确保您使用的是定制的小区标识是正确的 –

+0

是的,这是正确的数据来了,但细胞的高度是不妥当的。 – Krunal

+0

可以在这个CGFloat height = MAX(size.height,55.0)中打印高度; –

回答

1

你是制约串电池减去的细胞保证金2倍的全宽度和2000年的最大高度的大小,但由于你是设置字符串作为标签的文本使用绿色的文字颜色,您应该根据标签的宽度和任意2000或任意选择的高度来检查尺寸。

CGSize constraint = CGSizeMake(MYGREENLABEL_WIDTH, 2000); 
+0

在'heightForRowAtIndexPath'写这仍然没有工作 '静态的NSString * CellIdentifier = @ “BTSTicketsCellIdentifier”; CRIndCoupnCell * cell =(CRIndCoupnCell *)[tblDescriptn dequeueReusableCellWithIdentifier:CellIdentifier]; NSString * text = [DescArr objectAtIndex:[indexPath row]]; CGSize约束= CGSizeMake(cell.lblRight.frame.size.height,2000.0f); CGSize尺寸= [文本sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:约束lineBreakMode:UILineBreakModeWordWrap]; CGFloat height = MAX(size.height,200.0f); 返回高度;' – Krunal

+0

不要将此代码放在'heightForRowAtIndexPath'方法中。只需从笔尖检查您的'lblRight'框架并将其宽度设置为约束尺寸,那就是 – Zen

+0

在哪里设置'lblRight'框架?在哪种方法? – Krunal