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

问题描述 投票:1回答:1

我正在创建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];
uitableview uiimageview uilabel cgrect
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)];
© www.soinside.com 2019 - 2024. All rights reserved.