Expandable view within tableview

Viewed 221

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

2 Answers

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.

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

Related