So, after going through many examples, I finally realized what happened. Its one of those things where some missing lines ruined the whole code. For me it was
cell.bodyLabel.preferredMaxLayoutWidth = tableView.bounds.size.width;
which we need to add to the method heightForRowAtIndexPath or it gives a constraint error.
I got the solution thanks to one of the comments on the amazing answer here . Although in the sample code provided in the answer, the line above is given in the cellForRowAtIndexPath function instead of heightForRowAtIndexPath. But my code could not work that way.
So, the base code:
//RJCell.h
//RJCell.m
#import "RJTableViewCell.h" @interface RJTableViewCell () @property (nonatomic, assign) BOOL didSetupConstraints; @end @implementation RJTableViewCell - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { // Initialization code self.titleLabel = [[UILabel alloc] init]; [self.titleLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; [self.titleLabel setLineBreakMode:NSLineBreakByTruncatingTail]; [self.titleLabel setNumberOfLines:1]; [self.titleLabel setTextAlignment:NSTextAlignmentLeft]; [self.titleLabel setTextColor:[UIColor blackColor]]; [self.titleLabel setBackgroundColor:[UIColor clearColor]]; [self.contentView addSubview:self.titleLabel]; // Add this label to the button self.bodyLabel = [[UILabel alloc] init]; [self.bodyLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; [self.bodyLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical]; [self.bodyLabel setLineBreakMode:NSLineBreakByTruncatingTail]; [self.bodyLabel setNumberOfLines:0]; [self.bodyLabel setTextAlignment:NSTextAlignmentLeft]; [self.bodyLabel setTextColor:[UIColor darkGrayColor]]; [self.bodyLabel setBackgroundColor:[UIColor clearColor]]; [self.contentView addSubview:self.bodyLabel]; } return self; } - (void)updateConstraints { [super updateConstraints]; if (self.didSetupConstraints) return; // Get the views dictionary NSDictionary *viewsDictionary = @{ @"titleLabel" : self.titleLabel, @"bodyLabel" : self.bodyLabel }; NSString *format; NSArray *constraintsArray; //Create the constraints using the visual language format format = @"V:|-10-[titleLabel]-10-[bodyLabel]-10-|"; constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:viewsDictionary]; [self.contentView addConstraints:constraintsArray]; format = @"|-10-[titleLabel]-10-|"; constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:viewsDictionary]; [self.contentView addConstraints:constraintsArray]; format = @"|-10-[bodyLabel]-10-|"; constraintsArray = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:viewsDictionary]; [self.contentView addConstraints:constraintsArray]; self.didSetupConstraints = YES; } @end
//RJTableViewController.h
//RJTableViewController.m
Basically, the hard part is in two places, setting limits first. Secondly, the implementation of the methods heightForRowAtIndexPath and cellForRowAtIndexPath.
Avnish gaur
source share