Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell cell reuse and UILabel auto-layout dynamic sizing

I'm working with a subclassed UITableViewCell and I need to align a UILabel's text to the Top Left all while using Auto Layout. I realize reading up that sizeToFit really shouldn't be used with Auto Layout and I'd like to avoid it, and somehow use a constraint. Basically the label's text is reset every reuse of the cell so the sizing would need to be dynamic on reuse.

Here's the Lazy initializer label inside the subclassed cell:

- (UILabel *)commentsLabel {

    if (_commentsLabel == nil) {

         _commentsLabel = [[UILabel alloc] init];
         [_commentsLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
         _commentsLabel.numberOfLines = 0;
         _commentsLabel.lineBreakMode = NSLineBreakByWordWrapping;
         _commentsLabel.preferredMaxLayoutWidth = 100;
    }

    return _commentsLabel;
}

There are auto-layout constraints being set on the label (commentsLabel is a subView added to self.customView on the cell subclass):

[self.customView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-locationToTopPadding-[locationLabel(locationHeight)]-locationToCommentsPadding-[commentsLabel]-commentsToBottomPadding-|"
                                                                        options:0
                                                                        metrics:@{
                                                                                  @"locationToTopPadding":@(locationToTopPadding),
                                                                                  @"locationHeight":@(locationHeight),
                                                                                  @"locationToCommentsPadding":@(locationToCommentsPadding),
                                                                                  @"commentsToBottomPadding":@(commentsToBottomPadding)
                                                                                  }
                                                                          views:viewsDictionary]];

[self.customView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[thumbnailImageView]-[commentsLabel]-|"
                                                                        options:0
                                                                        metrics:@{@"commentsWidth":@(commentsWidth)}
                                                                          views:viewsDictionary]];

setting just:

self.commentsLabel.preferredMaxLayoutWidth = 100;

doesn't seem to work although that is mentioned in most answers.

currently have this implemented but not seeing any results. UILabel sizeToFit doesn't work with autolayout ios6

another answer I tried. UILabel sizeToFit only works with AutoLayout turned off

I feel that I'm just missing 1 constraint that can be added in addition to the constraint above but the programmatic constraints I try to add don't work or throw an exception. I'm working completely in code there are no xibs.

ETA: tried setting a height constraint inside the existing vertical constraint line:

like suggested here: Dynamically change UILabel width not work with autolayout

in the vertical constraint line above making it:

-[commentsLabel(>=30@900)]-

I've messed around with the height value and priority value and nothing changed.

ETA: making some progress, I think it has to do with the bottom padding of the label, tried this and some of the labels are aligned correctly some aren't:

->=commentsToBottomPadding-
like image 528
Mark W Avatar asked Nov 11 '22 14:11

Mark W


1 Answers

solution in the event any one else runs into the same issue with a UITableViewCell and a custom Label that changes dynamically:

-[commentsLabel]->=yourBottomPadding-

put that in the vertical constraint

call this in the viewController after you set the text:

[cell updateConstraints];
like image 187
Mark W Avatar answered Nov 15 '22 04:11

Mark W