Preferred approach for modifying UITableViewCell content based on cell width

I have a dynamic custom UITableViewCell that uses auto-layout.

I change the text content of the label, depending on the width of the cell.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ... cell.someLabel.text = CGRectGetWidth(cell.bounds) < 321 ? @"Concise" : @"Much longer blurb"; return cell; } 

reloadData is called during the orientation transition to update the label.

While this works, I believe there is a better way to handle this, which is not related to the overhead of reloading the tableView.

(None of the restrictions need to be changed. The label is in one place, but just displays different content when there is more space for this.)

  • Should I subclass UILabel and change the text in -didMoveToSuperview ?

  • Can I change text in -layoutSubViews ? (Won't this lead to another layout pass because the text has been changed?)

Thanks for any answers!

+7
ios objective-c uitableview autolayout ios8
source share
1 answer

Just adding this logic to layoutSubviews should do the trick. It is called every time the auto-linking engine performs its task.

 -(void)layoutSubviews { self.someLabel.text = CGRectGetWidth(self.bounds) < 321 ? @"Concise" : @"Much longer blurb"; [super layoutSubviews]; } 
+4
source share

All Articles