当imageview作为uitableviewcell的子视图添加时,如何使imageview成为可伸缩的

时间:2013-05-30 13:58:40

标签: objective-c uitableview chat stretched

如果我在表格单元格中写两行以上,我想使imageview成为可伸缩的。 代码如下:

任何帮助都会得到满足。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
chatCell *cell = (chatCell *)[tableView dequeueReusableCellWithIdentifier:CHAT_CELL_IDENTIFIER];
NSUInteger row = indexPath.row;
if (row < chatData.count)
{
    self.bubbleImage = [[UIImageView alloc] init];
    self.bubbleImage.frame = CGRectMake(0,22,250,66);
    self.bubbleImage.image = [[UIImage imageNamed:@"bubbleMine.png"] stretchableImageWithLeftCapWidth:21 topCapHeight:14];

    NSString *chatText = [[chatData objectAtIndex:row] objectForKey:TEXT];
    cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
    UIFont *font = [UIFont systemFontOfSize:14];
    CGSize size = [chatText sizeWithFont:font constrainedToSize:CGSizeMake(150.0f, 1000.0f) lineBreakMode:NSLineBreakByCharWrapping];
    cell.textString.frame = CGRectMake(75, 18, size.width +20, size.height + 20); // set text frame
    cell.textString.font = [UIFont fontWithName:FONT_NAME size:FONT_SIZE];        // set text font
    cell.textString.text = chatText;                                              // set text
    [cell.textString sizeToFit];

    NSDate *theDate = [[chatData objectAtIndex:row] objectForKey:DATE];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:DATE_FORMAT];
    NSString *timeString = [formatter stringFromDate:theDate];
    cell.timeLabel.text = timeString;                                       // set timeLabel to display date and time
    cell.userLabel.text = [[chatData objectAtIndex:row] objectForKey:NAME]; // set userLabel to display userName

    [self.bubbleImage addSubview:cell.userLabel];
    [self.bubbleImage addSubview:cell.timeLabel];
    [self.bubbleImage addSubview:cell.textString];

    [cell addSubview:self.bubbleImage];
}
return cell;
}

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

您需要调整bubbleImage的帧高。尝试利用单元格内textString的最大Y来计算bubbleImage的最终高度:

self.bubbleImage.frame = CGRectMake(0,22,250,CGRectGetMaxY(cell.textString.frame)+10.0);

还要记住计算表格视图单元格每行的高度。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //calculate your height for each row based on the textString
    UIFont *font = [UIFont systemFontOfSize:14];
    CGSize size = [chatText sizeWithFont:font constrainedToSize:CGSizeMake(150.0f, 1000.0f) lineBreakMode:NSLineBreakByCharWrapping];
    return size.height+20.0; //or whatever padding value you need
}