简体   繁体   中英

Is it possible to implement tableview inside tableview cell in swift 3?

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!

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM