1

I have a UITableViewCell with a description label pinned to the bottom as shown below: enter image description here

Tapping on the description label toggles the numberOfLines between 3 and 0:

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupView];
}

-(void) setupView
{

    UITapGestureRecognizer * gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggleNumberOfLines)];
    self.jobDescriptionLabel.userInteractionEnabled = YES;
    [self.jobDescriptionLabel addGestureRecognizer:gestureRecognizer];
}

- (void) toggleNumberOfLines {
    if(self.jobDescriptionLabel.numberOfLines != 0){
        self.jobDescriptionLabel.numberOfLines = 0;
    }else{
        self.jobDescriptionLabel.numberOfLines = kNumberOfLines;
    }
    [self.jobDescriptionLabel sizeToFit];
    [self layoutIfNeeded];
}

When I tap on the label, the number of lines does change but the cell does not expand to accommodate the new number of lines. How do I fix this?

Collapsed (Default):

enter image description here

Expanded:

enter image description here

W.K.S
  • 9,787
  • 15
  • 75
  • 122
  • Still unable to understand your actual problem. Issue is with label or with expanded tableView?? – dahiya_boy Apr 05 '17 at 12:36
  • `UITableViewAutomaticDimension` automatically calculates the height for the cell based on its contents. In my case, I am updating the height of the label but the tableview does not update the height of the cell. How do I get the tableview to update the height of the cell – W.K.S Apr 05 '17 at 12:41
  • You need to reload the table forcefully. First `HeightOfRow` method will call then `cellForRow`. So when the `HeightOfRow` method called make sure you have a new Label height. And please add your code work done regarding this. – dahiya_boy Apr 05 '17 at 12:53

3 Answers3

4

If you have your constraints set up correctly, you do not need to reload the data --- not the whole table, not even the affected rows.

Best method is to add a delegate function back to your tableview controller, and just call these lines back-to-back:

tableView.beginUpdates()
tablebleView.endUpdates()

That will tell auto-layout to re-calc the row heights.

Edit: Check my answer - which includes a link to a demo project - here: Expand UILabel inside UITableView with "more" button like Instagram

Community
  • 1
  • 1
DonMag
  • 69,424
  • 5
  • 50
  • 86
0

When the cell lays out it's subviews, the table view doesn't actually have any way of knowing anything has changed. You manually have to tell the table view to recalculate.

You probably want to reload the table view, or at least the cell the changes are happening in.

Take a look at reloadData for reloading the whole table view, or reloadRowsAtIndexPaths: if you want to just reload specific indexes in the Apple reference docs.

Matthew Hallatt
  • 1,310
  • 12
  • 24
  • The problem with this approach is that when I call `reloadRowsAtIndexPaths ` it recreates the cell. This means that I need to store the state `jobDescriptionExpanded` in my `UIViewController` and pass it when the cell is created. While this works, it's not exactly elegant – W.K.S Apr 05 '17 at 13:30
  • Why not use the selected state of the `UITableViewCell` to decide the value for `jobDescriptionExpanded`? Unfortunately, the only way to have the `UITableView` update the height of its cell is to reload cells. So no matter how 'inelegant' you think your solution might be, you will have to do it. – Matthew Hallatt Apr 05 '17 at 14:27
0

You can update the tableview cell height in heightForRowAtindexPath:. Call reloadRowsAtIndexPath Method to update single row of UITableView.

self.dataTableView.beginUpdates()
self.dataTableView.reloadRows(at: [IndexPath(row: 0, section: 0)], with: UITableViewRowAnimation.automatic)
self.dataTableView.endUpdates()
MBN
  • 304
  • 1
  • 12