I am trying to make a chat application.I populate the chat bubbles from nib.
But whenever I add a new message to the array and call reloadData method, the cells above it shrink.
Here is my code for 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;
This is the code for 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;
Also here is the code for SentMessageTableViewCell xib implementation :
- (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;
}
But when I run the app, the result is something like this.
The cell above shrinks to default size.
EDIT : Here is my xib.
Thanks in advance for the help.