1

我正在尝试制作一个聊天应用程序。我从笔尖填充聊天气泡。

但是每当我向数组中添加一条新消息并调用 reloadData 方法时,它上面的单元格就会缩小。

这是我的 UITableView cellForRowAtIndexPath 代码:

    SentMessageTableViewCell *sentMessageCell;
    sentMessageCell = [tableView dequeueReusableCellWithIdentifier:@"SentChatMessage"];

    if(sentMessageCell == nil)
    {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"SentChatMessage" owner:self options:nil];
        sentMessageCell = [topLevelObjects objectAtIndex:0];
    }
    sentMessageCell.message = message.body;
    [sentMessageCell initWithMessage:message.body andDateTimeStamp:sendDateTimeStamp];
    return sentMessageCell;

这是 heightForRowAtIndexPath 的代码

CGRect expectedLabelSize = [messageBody boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:13]} context:nil];

if(expectedLabelSize.size.height > 15)
    return expectedLabelSize.size.height + 55;
else
    return 65;

这里也是 SentMessageTableViewCell xib 实现的代码:

- (void)awakeFromNib {
    // Initialization code
}

- (void) layoutSubviews{
    [super layoutSubviews];

    CGRect expectedLabelSize = [self.message boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:13]} context:nil];

    if(expectedLabelSize.size.height > 15)
    {
        [LabelMessage setFrame:CGRectMake(50, 30, 200, expectedLabelSize.size.height + 15)];
        [ImageViewChatBox setFrame:CGRectMake(40, 10, 220, expectedLabelSize.size.height + 45)];
    }

    [LabelMessage setText:self.message];
    [LabelDateTimeStamp setText:self.dateTimeStamp];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

- (void) initWithMessage:(NSString *)message andDateTimeStamp:(NSString *)dateTimeStamp{
    self.message = message;
    self.dateTimeStamp = dateTimeStamp;
}

但是当我运行应用程序时,结果是这样的。

在此处输入图像描述

上面的单元格缩小到默认大小。

编辑:这是我的 xib。

在此处输入图像描述

在此先感谢您的帮助。

4

2 回答 2

2

你需要改变你的layoutSubviews方法

- (void) layoutSubviews{
    [super layoutSubviews];

    CGRect expectedLabelSize = [self.message boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:13]} context:nil];

    if(expectedLabelSize.size.height > 15)
    {
        [LabelMessage setFrame:CGRectMake(50, 30, 200, expectedLabelSize.size.height + 15)];
        [ImageViewChatBox setFrame:CGRectMake(40, 10, 220, expectedLabelSize.size.height + 45)];
    } else {

          // Need to set frame for normal height messages
    }

    [LabelMessage setText:self.message];
    [LabelDateTimeStamp setText:self.dateTimeStamp];
}
于 2015-07-28T12:01:47.600 回答
1

原评论:能不能给里面的元素加一些约束试试?

答: xib 文件缺少约束。iOS 需要正确的约束来适应框架(在这种情况下是高度变化)变化后的内部元素。

于 2015-07-29T14:13:19.393 回答