如何将UIImage设置为UITableView Cell,根据我在UILabel中输入的文本动态计算单元格高度?

时间:2012-09-21 05:18:21

标签: uitableview uiimageview uilabel cgrect

我正在创建UITableView,其中每个单元格大小将根据我输入的文本计算。 我正在计算heighForRowAtIndexPath中的Cell大小

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    NSString *text = [tableData objectAtIndex:[indexPath row]];

    CGSize constraint = CGSizeMake(210.0f, 20000.0f);
    size = [text sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    height = MAX(size.height, 44.0f);

    return height + (CCM * 2);
}

这就是我计算每个单元格高度的方法,现在我的问题是 - 我想将图像添加到每个单元格,如评论框?像这样 Click here to see comment boxes I am looking for

您可以看到这些评论框 我做的是把我的形象分成两部分 第一个是顶部,它在每个单元格上运行良好,起始图像即将到来 现在在修复底部我面临问题 对于单元格中的小文本UILable它运行良好 当UILabel中文本的大小增加时,它没有得到适当的调整

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"cell";

    NSLog(@"index path %d", indexPath.row);

    MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) 
    {
        cell = [[[MyTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    [cell.cellBackgroundBottom setFrame:CGRectMake(75.0f, 92.0f, 228.0f, height + 25.0f)];

    [cell.cellContent setText:(NSString*)[tableData objectAtIndex:[indexPath row]]];
    [cell.cellContent sizeToFit];

    CGRect theFrame = [cell.cellContent frame];
    theFrame.size.width = 210.0f;
    [cell.cellContent setFrame: theFrame];

    return cell;
}

我创建了一个命名MYTableCell的UITableView子类,我已经在每个单元格中定义了所需的内容。

 // Creating UILabel for cell content or comments block

        cellContent = [[UILabel alloc]initWithFrame: CGRectMake( 90.0f, 15.0f, 210.0f, 60.0f)] ;
        [cellContent setLineBreakMode:UILineBreakModeWordWrap];
        cellContent.numberOfLines = 0;
        [cellContent setMinimumFontSize:15.0f];
        [cellContent setBackgroundColor:[UIColor clearColor]];
        //   [cellContent setTextColor:[UIColor colorWithRed:73.0f green:73.0f blue:73.0f alpha:1.0f]];
        [cellContent setFont:[UIFont fontWithName:@"Helvetica" size:12.0f]];
        cellContent.textAlignment = UITextAlignmentLeft;
        [cellContent setTag:1];
        [self addSubview:cellContent];
        [cellContent release];

        cellBackgroundTop = [[UIImageView alloc] initWithFrame:CGRectMake(60.0f, 5.0f, 243.0f, 13.5f)];
        [cellBackgroundTop setImage:[UIImage imageNamed:@"comments.png"]];
        [self addSubview:cellBackgroundTop];
        [cellBackgroundTop release];

        cellBackgroundBottom = [[UIImageView alloc] initWithFrame:CGRectZero];
        [cellBackgroundBottom setImage:[UIImage imageNamed:@"Untitled-1_09.png"]];
        [self addSubview:cellBackgroundBottom];
        [cellBackgroundBottom release];

1 个答案:

答案 0 :(得分:0)

我的问题得到了回答 。这是我在cellForRowAtIndexPath

中必须做的
 NSString *text = [tableData objectAtIndex:[indexPath row]];

    CGSize constraint = CGSizeMake(210.0f, 20000.0f);

    CGSize size1 = [text sizeWithFont:[UIFont systemFontOfSize:12.0f] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

    CGFloat height1 = MAX(size1.height, 44.0f);

    [cell.cellBackgroundBottom setFrame:CGRectMake(75.0f, 18.0f, 228.0f, height1 + 10.0f)];