Shikha Sharma
Shikha Sharma

Reputation: 469

How to add a subview to a TableViewCell?

I need to add a subview to TableViewCell when the button in TableViewCell is clicked it should show another tableview as a subview and height of the tableview should be dynamic according to number of cell.

How can I do this?

How can I do this

Above screenshot is taken from a very popular shopping app and I need to do the same in my project.

Upvotes: 1

Views: 431

Answers (4)

foolishBoy
foolishBoy

Reputation: 351

You can use just one table view with UITableViewStyleGrouped style, and set "Men","Women","Kids & Baby" as TableView sections header, keep an boolean value to determine the result of "numberOfRowsInSection:" of each section and reload tableview.

Upvotes: 1

Kuldeep Tanwar
Kuldeep Tanwar

Reputation: 3526

You need to do some work on your own I can give the directions that'll give you a way from my point of view :-

  1. Make a Custom cell which you want to expand.
  2. While designing the cell make its height in storyboard like 200 or so according to your need and add all the elements those you want to see when the cell is expanded.

  3. You'll need two delegate methods first -didSelectItemAtIndexPath and second HeightForRowAtIndexPath at index path.

  4. First You need to make sure the user taps on the button or cell that you want to expand , and to achieve that you need to call didSelectItemAtIndexPath.

  5. Once you get your cell Position, in HeightForRowAtIndexPath check the indexpath is equal to your cell's indexpath,if yes then return the exact height(ie:200) of your cell otherwise return the default(ie:70) height of you cell.

Note : In didSelectItemAtIndexPath you need to call to method to update the current cell

[self.tableView beginUpdates];
[self.tableView endUpdates];

Upvotes: 0

Er. Vihar
Er. Vihar

Reputation: 1555

You can easily achieve this using simple UITableView. For that you need to set all your main categories as your UITableview section and respected sub-categories can be added to respective rowOfSection.

Upvotes: 3

Mehul Thakkar
Mehul Thakkar

Reputation: 12594

See the below link at github:

https://github.com/OliverLetterer/SLExpandableTableView

This contains the Expandable TableView, which you required. You have to implement SLExpandableTableViewDelegate and SLExpandableTableViewDatasource which contains different method, in which you have to provide inner tableview as well.

Hope this helps you.

Upvotes: 0

Related Questions