我正在尝试制作一个聊天应用程序。我从笔尖填充聊天气泡。
但是每当我向数组中添加一条新消息并调用 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。
在此先感谢您的帮助。