Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Kieran Robinson
Kieran Robinson
9,411 Points

resizing a UITableViewCell on button press.

In my app, i have a custom tableViewCell with a label at the bottom and an image at the top. I have set the number of lines on the label to 3 and if the text in the label exceeds this, it just has '...' at the end, however when a user clicks 'read more' I would like the cell to expand and the full contents of the label to be displayed. I have searched high and low on the internet and most of their suggestions have been depreciated. Any help is much appreciated. Kieran

3 Answers

You have to modify the view cell with this method

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  int height = 80;

  // Logic when the user pressed a button click more
 if (self.readMore == true)
     height = 100;

  return height; 
}
Kieran Robinson
Kieran Robinson
9,411 Points

thank you for the reply Camillo! I have tried what you said, I have set the height initially to 80, but i am having trouble writing the logic to add the logic for the true/false of the button press. My button to expand is called "expandButton" so would i replace self.readmore with self.expandButton?

In that case you need a protocol in a custom UITableViewCell class. And when the "Read More" Button is clicked you need to set the selected state in an array.

Here's a Sample Code

https://www.dropbox.com/s/fnzbvnpc1miqemw/ReadMoreCell.zip

Kieran Robinson
Kieran Robinson
9,411 Points

brilliant mate, works perfectly!