Expandable collapsable UITableView in iOS – Swift

Expandable and collapsable UITableView in iOS sdk using Swift3 - Tutorial

Introduction to Expandable and Collapsable UITableView

In this tutorial, we are going to create UITableView that will expand and collapse on tap of row or on row selection using Swift3 language for iOS app development. In more common words, showing sub-menu/ sub-items of main menu/item. Example is shown in the below given picture

       

Steps to create Expandable and collapsable UITableView

Step 1: Download the files already created with expandable and collapsable functionality. Download those files from Download Expandable and collapsable UITableView Files
Step 2:  Add the above downloaded files to your project, check mark “Copy items if needed”. 
Step 3: We will create our datasource in parent child relationship. Because the classes/files downloaded by us in last step based on parent child relation. The datasource will have parent as of type String and child will be of type Array containing String type values/elements. Below is the code that shows how to create data source for the expandable and collapsable UITableView


Step 4: To use it we will create an object of ExpandableTable and give it frame with 20 pixels gap from top.
4.1) After giving frame we will assign datasource created in last step to dataSourceForPlainTable property of ExpandedTable object.
4.2) Assign delegate to self, as we want to get notified whenever a selection is made by us.
4.3) Finally we will add it as subview to our main viewController. Below is the complete code



Step 5: Run your app and you will see the Expandable and collapsable UITableView. Tap on sub menu/ sub-item. That name will get logged on your project console.

Where to go from here

In this tutorial we learned about how to create Expandable and collapsable UITableView in iOS sdk using Swift3. You can download the full source code from here. Stay tuned for more tutorials…