Search code examples
iosswiftuitableviewsectionsexpandable-table

UITableView : Nested Section Swift 5


I am trying to implement a UITableView with the following structure :

struct DataArray {
    var mainsection : String
    var titlecontent : String
    var contentsection : [(question : String, answer : String)] = [(String, String)]()
}

var dataArray = [DataArrayArray]()

dataArray = [DataArray(mainsection : "Section 1","Section Title 1",[("question1","answer1"),("question2","answer2")]), 
DataArray(mainsection : "Section 2","Section Title 2",[("question1","answer1"),("question2","answer2")])]

The TableView should look like this and when the user clicks Section 1 ->expand the subsection (SectionTitle1 & SectionTitle2); when user clicks SectionTitle1 -> expand the subsection(Question1 & Question 2); When the user again clicks Question1 -> show the answer :

Section 1

  • Section Title 1

    • Question 1

      • Answer 1
    • Question 2

      • Answer 2

Section 2

  • Section Title 1

    • Question 1

      • Answer 1
    • Question 2

      • Answer 2

Can anyone help me with this or any other solution to solve this?


Solution

  • This type of TableView is generally called Accordion TableView. If you run google search for this you will find a lot of tutorials and sample codes. 2 links are mentioned below

    1. Making Simple Accordion TableView in swift?
    2. https://medium.com/ios-os-x-development/ios-how-to-build-a-table-view-with-collapsible-sections-96badf3387d0