-2

I want to create a view like this to show some long text. got some pods for this but i want to this do programatically.

thanks in advance.

first image -> the view before tap.

second image -> view after tap.

first image second image

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Prateek Regar
  • 11
  • 2
  • 5
  • 1
    Did you try anything for yourself? Any research? From your pics, it's about expanding the `UITableViewCell`. You should read about `UITableView` and `UITableViewCell`. It's actually very easy to accomplish. – Fahri Azimov Oct 30 '17 at 09:48
  • you can also try third party library like this :https://github.com/jeantimex/ios-swift-collapsible-table-section – Jayesh Thanki Oct 30 '17 at 09:51
  • Show what you have already tried. – PGDev Oct 30 '17 at 10:52

1 Answers1

0

If you need a quick working solution, you can try this.

create a TableViewCell with two labels: title label with Fixed height (let's say 48) and description label (multiline)

Maintain heights for each cell in an array, by default keep it the height of title label, just to show the title label (say itemHeights = [48,48,48]

When you select cell, inside the delegate method "didSelectRowAt:", toggle the height

if self.itemHeights[indexPath.row] == UITableViewAutomaticDimension {
            self.itemHeights[indexPath.row] = 48
} else {
            self.itemHeights[indexPath.row] = UITableViewAutomaticDimension
}
self.tableView.reloadRows(at: [indexPath], with: .automatic)

Inside delegate method "heightForRowAt:", return the updated height

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return self.itemHeights[indexPath.row]

}

Note: this is just a workaround which gives you almost the same result but with some jerky animation.

Rikesh Subedi
  • 1,755
  • 22
  • 21