UITableviewCell row height shrinking

315 views Asked by At

I have a UITableViewCell with two labels and a switch. When the switch value changes to state Off I remove one of the labels and when changed to On its added back the to the view. I have used auto layout. So when I wanted to add back I added back the lost constraints while removing. Here is my code snippet

- (void)willUpdateCell:(SampleTableViewCell *)cell{
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

if ([cell.messageSwitch isOn]) {
    cell.messageDescription = [UILabel new];
    cell.messageDescription.translatesAutoresizingMaskIntoConstraints = NO;
    cell.messageDescription.text = self.messages[indexPath.row][@"description"];
    cell.messageDescription.numberOfLines = 0;
    cell.messageDescription.lineBreakMode = NSLineBreakByWordWrapping;
    [cell.contentView addSubview:cell.messageDescription];


    cell.left = [NSLayoutConstraint constraintWithItem:cell.messageDescription
                                             attribute:NSLayoutAttributeTrailing
                                             relatedBy:NSLayoutRelationEqual
                                                toItem:cell.messageSwitch
                                             attribute:NSLayoutAttributeTrailing
                                            multiplier:1.0
                                              constant:0];
    cell.bottom = [NSLayoutConstraint constraintWithItem:cell.messageDescription
                                               attribute:NSLayoutAttributeBottom
                                               relatedBy:NSLayoutRelationEqual
                                                  toItem:cell.contentView
                                               attribute:NSLayoutAttributeBottom
                                              multiplier:1.0
                                                constant:15];

    cell.top = [NSLayoutConstraint constraintWithItem:cell.messageDescription
                                            attribute:NSLayoutAttributeTop
                                            relatedBy:NSLayoutRelationEqual
                                               toItem:cell.messageTitle
                                            attribute:NSLayoutAttributeTop
                                           multiplier:1.0
                                             constant:2];


    cell.right = [NSLayoutConstraint constraintWithItem:cell.messageDescription
                                              attribute:NSLayoutAttributeLeading
                                              relatedBy:NSLayoutRelationEqual
                                                 toItem:cell.contentView
                                              attribute:NSLayoutAttributeLeading
                                             multiplier:1.0
                                               constant:15];

    [cell.contentView addConstraints:@[cell.bottom,cell.left,cell.right,cell.top]];

}else{

    [cell.messageDescription removeFromSuperview];
}
 [cell updateFont];
 [cell.contentView updateConstraints];
 [UIView animateWithDuration:0.5 animations:^{
     [cell.contentView setNeedsLayout];
 }];
}

The issue I have is shrinking the height of the row when the state of the switch changes. The spacing still remains after the view is removed. If I tried to reload the table, the tableview behaves weirdly. I have attached my code here for your reference. Please suggest me what I can do to get the tableview shrink the row height when its subview is removed.

0

There are 0 answers