当imageview被添加为子视图的uitableviewcell时,将imageview设置为可伸缩时出现错误

问题描述:

如果我在表格单元中写入多于两行的图像,我希望将imageview设置为可伸缩。但我得到了如下问题:当imageview被添加为子视图的uitableviewcell时,将imageview设置为可伸缩时出现错误

我对细胞的代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    chatCell *cell = (chatCell *)[tableView dequeueReusableCellWithIdentifier:CHAT_CELL_IDENTIFIER]; 
    NSUInteger row = indexPath.row; 
    if (row < chatData.count) 
    { 

    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 = [[UIImageView alloc] init]; 
    self.bubbleImage.frame = CGRectMake(0,22,250,CGRectGetMaxY(cell.textString.frame)+10.0); 
    self.bubbleImage.image = [[UIImage imageNamed:@"bubbleMine.png"] stretchableImageWithLeftCapWidth:21 topCapHeight:14]; 

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

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSString *cellText = [[chatData objectAtIndex:indexPath.row] objectForKey:TEXT]; 
//calculate height for each row based on the textString 
UIFont *font = [UIFont fontWithName:FONT_NAME size:FONT_SIZE]; 
CGSize size1 = [cellText sizeWithFont:font constrainedToSize:CGSizeMake(150.0f, 1000.0f) lineBreakMode:NSLineBreakByCharWrapping]; 
return size1.height+100.0; //or whatever padding value you need 
} 

存在这个问题:

CGRectGetMaxY(cell.textString.frame)+10.0 

符合如下:

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

,因为我得到2/3图像的单细胞。

问题是,您每次重复使用时,都会不断向单元添加新的子视图。而不是仅仅做:

[cell addSubview:self.bubbleImage]; 

尝试:

[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 
[cell.contentView addSubview:self.bubbleImage]; 

一个更好的选择将是为bubbleImage创建一个单一UIImageView创建并设置其自动尺寸屏蔽单元格时。然后,当细胞被调整的图像会自动进行适当的变更......

+0

谢谢。有用。 – Ponting