0
votes

I have dynamic cell for chat table. I have message label and time lable. Contraints are as follows

message label leading >=100

message label trailing = 15

message label top =10

message label bottom to time label top = 5

time label to bottom = 5

time label width 60 and height 21

time label trailing =15

I tried lot with content Hugging and comprassion values. Message label Content Hugging property is

Horizontal 250

Vertical 1000

Compression Resistance

Horizontal 250

Vertical 1000

It correctly calculates height first time. But when I add new cell when message is send or received, go back and come back to same view, its height increases more 1500. Here is code to find height for row.

-(CGFloat)heightForCellAtIndexPath:(NSIndexPath*)indexPath
{
    MessageCentreService *message = [[self.dic_messages objectForKey:[self.array_dates objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];

    static dispatch_once_t onceToken;
    static AttendieChatTableViewCell *sizingRightCell = nil;
    static AttendieChatTableViewCell *sizingLeftCell = nil;
    dispatch_once(&onceToken, ^{
        sizingLeftCell = [self.tableView_attendeeChatList dequeueReusableCellWithIdentifier:kCellChatLeftIdentifier];
        sizingRightCell = [self.tableView_attendeeChatList dequeueReusableCellWithIdentifier:kCellChatRightIdentifier];
    });


    sizingLeftCell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight(sizingLeftCell.bounds));
    sizingRightCell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.tableView_attendeeChatList.frame), CGRectGetHeight(sizingRightCell.bounds));

    if(message.message_type == 1)
    {
        sizingLeftCell.label_message.text = message.message_text;
        sizingLeftCell.label_messageTime.text = @"10.30 pm";

        [sizingLeftCell setNeedsLayout];
        [sizingLeftCell layoutIfNeeded];

        CGSize size = [sizingLeftCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

        return size.height;
    }
    else
    {
        sizingRightCell.label_message.text = message.message_text;
        sizingRightCell.label_messageTime.text = @"10.30 pm";
        [sizingRightCell setNeedsLayout];
        [sizingRightCell layoutIfNeeded];
        CGSize size = [sizingRightCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
        return size.height;
    }
}
1

1 Answers

0
votes

I also had problems with systemLayoutSizeFittingSize, i fixed that by setting the preferredMaxLayoutWidth of labels.