# Creating UITableView with parent-child cells (Tree Structure)

**URL:** <https://forums.swift.org/t/creating-uitableview-with-parent-child-cells-tree-structure/64672>\
**Category:** Using Swift\
**Tags:** off-topic, uikit, tableview\
**Created:** [April 28, 2023, 10:33am UTC](https://forums.swift.org/t/creating-uitableview-with-parent-child-cells-tree-structure/64672 "2023-04-28T10:33:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ganaraj-savant](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ganaraj-savant/32/24725_2.png) [@ganaraj-savant](https://forums.swift.org/u/ganaraj-savant)\
**Post date:** [April 28, 2023, 10:33am UTC](https://forums.swift.org/t/creating-uitableview-with-parent-child-cells-tree-structure/64672/1 "2023-04-28T10:33:36Z")

</div>

Hi, I'm trying to implement a custom tableview which when tapped on a cell should expand and display its child cells, which further might have their children. Tapping again on the cell should collapse the view.

My data model looks something like below:

```swift
var objectTree = [
    MyObject(name: "Parent 1", id: "1", parentId: nil, children: [MyObject(name: "Child 1", id: "2", parentId: "1"), MyObject(name: "Child 2", id: "3", parentId: "1"),]),
    MyObject(name: "Parent 2", id: "4", parentId: nil, children: [MyObject(name: "Child 3", id: "5", parentId: "4", children: [MyObject(name: "Grandchild 1", id: "6", parentId: "5")])])
]

```

Here, "Parent 1" is the first cell, when tapped on it should expand and display its `children` "Child 1" & "Child 2" as sub/child cells under the "Parent 1" cell forming a tree-like structure.

Is there a way I can achieve this?

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [April 28, 2023, 10:44am UTC](https://forums.swift.org/t/creating-uitableview-with-parent-child-cells-tree-structure/64672/2 "2023-04-28T10:44:13Z")

</div>

Hello @ganaraj-savant

Those forums are about Swift, the programming language, and not about Swift libraries such as UIKit or SwiftUI. In the future, please ask similar questions on the Apple developer forums, or stack overflow, and only use those forums for Swift-related questions only.

Now, I can't resist providing a quick answer: UITableView does not provide any parent-child feature, so you'll have to use elbow grease, code it yourself, or maybe look for a ready-made solution online. On the other hand, UICollectionView has built-in support for parent-child cells. Apple made an example app that you can download from [Implementing Modern Collection Views](https://developer.apple.com/documentation/uikit/views_and_controls/collection_views/implementing_modern_collection_views). If you have further questions on this particular topic, remember to ask them in some other forums, thanks :-)

---

<div class="post-metadata">

**Author:** ![henrikac](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/henrikac/32/28038_2.png) [@henrikac](https://forums.swift.org/u/henrikac)\
**Post date:** [April 29, 2023, 2:03pm UTC](https://forums.swift.org/t/creating-uitableview-with-parent-child-cells-tree-structure/64672/3 "2023-04-29T14:03:24Z")

</div>

> [@gwendal.roue](#):
>
> Those forums are about Swift, the programming language, and not about Swift libraries

> [@gwendal.roue](#):
>
> and only use those forums for Swift-related questions only

and yet there are dedicated tags for frameworks like Kitura and Vapor, and a library like Alamofire 🙂

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [April 29, 2023, 2:28pm UTC](https://forums.swift.org/t/creating-uitableview-with-parent-child-cells-tree-structure/64672/4 "2023-04-29T14:28:41Z")

</div>

Indeed one can find Kitura, Vapor, and Alamofire-related posts here, because those organisations have opted in for dedicated subforums ("categories") here. Other projects are hosted here: see the list of [Related Projects](https://forums.swift.org/c/related-projects/25).

UIKit and SwiftUI, on the other hand, do not have any dedicated category here.

And the "Using Swift" category (the category of this thread) is about Swift-the-language, not UIKit or SwiftUI.

Now, I didn't make the rules, and I'm not a cop, and I just wanted to **help the OP get an accurate answer** to the original question. Pure UIKit and SwiftUI questions have a better chance of finding an extended answer elsewhere, that's how things are 🤷

---

<div class="post-metadata">

**Author:** ![henrikac](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/henrikac/32/28038_2.png) [@henrikac](https://forums.swift.org/u/henrikac)\
**Post date:** [April 29, 2023, 2:36pm UTC](https://forums.swift.org/t/creating-uitableview-with-parent-child-cells-tree-structure/64672/5 "2023-04-29T14:36:34Z")

</div>

I see 🙂 I'm still fairly new to this forum even though I have been a member for some time and I don't know all the rules and am just asking questions 🙂

Thank you for explaining 🙂

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [April 29, 2023, 3:32pm UTC](https://forums.swift.org/t/creating-uitableview-with-parent-child-cells-tree-structure/64672/6 "2023-04-29T15:32:40Z")

</div>

Thank you for asking :)
