2

I currently have a table view controller which consist of not only the cells but also a UIView. Now, within that UIView there's a label which might have more than 1 line of text with a See More button. When I pressed that button, the button itself will disappear and the text.numberOfLines is set to 0 so the View should expand to show all text. Which doesn't seems to work in my case, The button does disappear though and the text just continues to the edge of the screen, truncated instead of extending down.

But when this whole UIView is outside the table view controller the functions above work just as expected, but not after I've moved them to within the table View right above the prototype cells. Any Ideas?

i

bondgaide
  • 85
  • 7
  • You are using tableHeaderView, Please refer to this solution once https://stackoverflow.com/questions/20982558/how-do-i-set-the-height-of-tableheaderview-uitableview-with-autolayout – Arun Sep 09 '20 at 11:03

2 Answers2

2

When you are setting up the tableviewcell, you have to specify a fixed height for the row with the delegate function

func tableView(_ tableView: UITableView,   heightForRowAt indexPath: IndexPath) {
 if expandedArray[indexPath.row] {
  return 60
 } else {
  return UITableViewAutomaticDimension
 }
}

Then keep an array of bools, to specify if the cell is expanded or not. Primarily set the bools to false indicating "not expanded". Then when the use presses the "see more" button, make the bool in the array with the right index true, indicating that the cell is expanded. and call the below code updating the cell.

tableView.beginUpdates()
tableView.reloadRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
tableView.endUpdates()

That should do the trick. P.S: Don't forget to properly add constraints inside tableview cell.

  • The UIView I was talking about is not a cell, It's a UIView that I've dragged into the table view controller(not the cell itself, see in the hierarchy) so it's a view right before all the dynamic cells. I'm not sure whether it's even included in the indexPath. Perhaps I'm misunderstanding something? – bondgaide Sep 09 '20 at 09:49
1

Use UITableViewAutomaticDimension and reload your cell on click of see more button.

Prakash Shaiva
  • 1,047
  • 8
  • 12
  • It's a UIView I've dragged into the tableview controller right before all the dynamic cells. So reloading the cells doesn't reloads the UIView in question. So the view I was talking about is not a cell. Or is it? – bondgaide Sep 09 '20 at 09:52
  • then try adding it as a header/cell instead of just UIView. – Prakash Shaiva Sep 09 '20 at 11:05