Start a Project

Expandable Table View iOS Swift

 

Are you searching  “How to implement Expandable Tableview in iOS in Swift”

Then you are at the right place , here is the quick solution for you.

Just follow these steps to go further:-

  1. Take a UIViewController on the interface builder. Name it as ExpandableViewController.
  2. Drop a UITableView over it and assign the necessary constraints.
  3. Drop a UITableViewCell over the table and then a label inside it. Assign the constraints.

Now your ViewController is looking just like the below picture.

4. Assign the TableView’s Datasource and Delegate to the ExpandableViewController.

5. Assign “ExpandCell” as the UITableViewCell class.

 

Now paste the below code inside your ExpandableViewController class.

Don’t worry , I am explaining everything.

Now lets dive into the code , Read the following step by step explanation.

  1. var expandData = [NSMutableDictionary]() this is our data format.
  2.  self.expandData.append([“isOpen”:”1″,”data”:[“banana”,”mango”]])  the key “isOpen” is actually helping us to expand the table sections and key “data” is holding the cell data
  3. Inside the numberOfRowsInSection method we are checking the “isOpen” key value and returning the rows according to it.
  4. Inside the viewForHeaderInSection method we are creating a custom view for section header of tableView and assigning a UITapGestureRecognizer to it. This will help us to handle section taps.
  5. Whenever a user tap the section we are changing the “isOpen” key value and reloading the section this will result in changing the number of rows for that section , Hence we will see an expanding animation with the tableView section.

Finally after running the project the table will look like this.

Thats How we have created our Expandable UITableVIew.

Its done , enjoy…..

 

 

Exit mobile version