Expandable Table View iOS Swift

Updated 3 February 2018

Save

 

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…..

 

 

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home