1

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.

enter image description here

The cell above shrinks to default size.

EDIT : Here is my xib.

enter image description here

Thanks in advance for the help.

2 Answers2

2

You need to change your layoutSubviews method

- (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];
}
Ashish P.
  • 848
  • 5
  • 12
1

Original Comment: Could you add some constraints for the inside elements and try it?

Answer: There is missing constraints on xib file. iOS needs correct constraints to adapt inner elements after frame (height change in this case) changes.

Ersin Sezgin
  • 640
  • 5
  • 15