1

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

- (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);
}

这就是我计算每个单元格高度的方式,现在我的问题是 - 我想像评论框一样向每个单元格添加图像?像这样 点击这里查看我正在寻找的评论框

你可以看到这些评论框我所做的是我把我的图像分成两部分第一个有顶部,它在每个单元格上都运行良好,现在开始图像正在出现,同时修复底部我面临单元格 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];
4

1 回答 1

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)];
于 2012-09-21T07:29:34.550 回答