2

I'm having trouble with putting a tableview inside a uitableview cell. right now I am going to implement the comments and reply. and reply should be inside which you are going to comments. so I am going to implement comments of post in tableview cell, inside it reply of all that comments in other tableview cell. Any suggestion or other way to implement these scenario!

MiTal Khandhar
  • 275
  • 1
  • 4
  • 15
  • Possible duplicate of [Is it possible to add UITableView within a UITableViewCell](https://stackoverflow.com/questions/17398058/is-it-possible-to-add-uitableview-within-a-uitableviewcell) – ChrisTheGreat Oct 10 '17 at 19:51
  • A similar question was asked [here](https://stackoverflow.com/questions/17398058/is-it-possible-to-add-uitableview-within-a-uitableviewcell?rq=1). – ChrisTheGreat Oct 10 '17 at 19:52
  • 3
    While it's possible, it's generally not a good idea. Apple suggests you avoid nesting scrollable elements, because then they have to fight over which one received a given scroll gesture, and it's not a good user experience. This goes for a tableView inside a tableView, scrollView inside collectionView, et cetera. I would suggest emulating what many social media apps do, which is show 1 or 2 of the nested replies in a stack view with a tappable action to go to a different screen to view them all. – Connor Neville Oct 10 '17 at 20:04
  • How did you solved this? – perrohunter Feb 12 '19 at 04:16

2 Answers2

13

This Question is duplicate. Answer in Swift

First Approach:

  1. Create a subclass for child tableView and override intrinsicContentSize.

         class MyOwnTableView: UITableView {
        override var intrinsicContentSize: CGSize {
            self.layoutIfNeeded()
            return self.contentSize
        }
    
        override var contentSize: CGSize {
            didSet{
                self.invalidateIntrinsicContentSize()
            }
        }
    
        override func reloadData() {
            super.reloadData()
            self.invalidateIntrinsicContentSize()
        }
    }
    
  2. In Interface builder change the class of your child tableView to MyOwnTableView (subclass of UITableView).

  3. Set automatic row height for both the parent and child table view.

        tableView.estimatedRowHeight = 60.0;
        tableView.rowHeight = UITableViewAutomaticDimension;
    

Second Approach: 1. Create a height constraint with any value for child tableView and conect an IBOutlet that sets the child tableView height. 2. Set the height constraint's constant to tableView.contentSize.height

    self.tableViewHeight.constant = self.tableView.contentSize.height
Learner
  • 1,107
  • 12
  • 14
3

Yes this is possible however not a recommended way to implement. If you want to show replies below to each post along with the comment I would recommend you to implement through sections. Each section should have three types of cell as mentioned below:

  • PostCellView
  • ReplyCellView
  • CommentCellView

In this case number of section would be number of posts and total number of rows for each section would be 1(Post) + N(where N is total number of replies) + 1(Comment).

With this approach you can add/remove replies through animation that Apple provides which is easy to implement.

I hope this will help you, please feel free to ask if you have any concerns.

Dhawal Dawar
  • 354
  • 2
  • 8