简体   繁体   English

在Swift 3的tableview单元内可以实现tableview吗?

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

I'm having trouble with putting a tableview inside a uitableview cell. 我在将表视图放入uitableview单元格时遇到麻烦。 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. 因此,我将在tableview单元中实现发布的评论,在其中回复其他tableview单元中所有评论。 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 PostCellView
  • ReplyCellView ReplyCellView
  • CommentCellView 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). 在这种情况下,部分数将是帖子数,每个部分的行总数将是1(帖子)+ N(其中N是答复总数)+1(评论)。

With this approach you can add/remove replies through animation that Apple provides which is easy to implement. 通过这种方法,您可以通过Apple提供的动画来添加/删除回复,该动画易于实现。

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. 为子tableView创建一个子类,并覆盖internalContentSize。

      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). 在“界面”构建器中,将子tableView的类更改为MyOwnTableView(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. 第二种方法: 1.为子tableView创建一个具有任何值的高度约束,并连接设置子tableView高度的IBOutlet。 2. Set the height constraint's constant to tableView.contentSize.height 2.将高度约束的常量设置为tableView.contentSize.height

    self.tableViewHeight.constant = self.tableView.contentSize.height

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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