Ios 使UItableview单元格高度等于它';s子UItableview

Ios 使UItableview单元格高度等于它';s子UItableview,ios,iphone,uitableview,swift3,Ios,Iphone,Uitableview,Swift3,我有一个包含UItableview的UItableview单元格,我需要使该单元格的高度等于其子UItableview的高度。 下图解释了我需要做什么 以下是如何处理表头单元格。单元需要在情节提要中进行原型化和子类化(如果需要配置)。然后在表视图委托中重写以下函数。或者,您可以动态生成视图,并从viewForHeaderInSection返回该视图 override func tableView(_ tableView: UITableView, heightForHeaderInSection

我有一个包含UItableview的UItableview单元格,我需要使该单元格的高度等于其子UItableview的高度。 下图解释了我需要做什么


以下是如何处理表头单元格。单元需要在情节提要中进行原型化和子类化(如果需要配置)。然后在表视图委托中重写以下函数。或者,您可以动态生成视图,并从viewForHeaderInSection返回该视图

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 150
    }

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let headerCell = tableView.dequeueReusableCell(withIdentifier: "ConferenceHeaderCell") as! ConferenceDetailHeaderCell
        // configure cell 
        return headerCell
    }
对于细胞本身来说,情况非常相似:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "ConferenceUserCell", for: indexPath) as! ConferenceDetail_TVCell

        // Configure the cell...

        return cell
    }

根据具体情况,您可能需要实现“heightForRowAtIndexPath”

首先查看my ViewController,它有一个tableview(tblview)和一个UITableViewCell(CustomTableViewCell)

然后查看我的CustomTableViewCell,它在一个单元格中有一个表视图和一个标签。请参阅

class CustomTableViewCell: UITableViewCell {

    static let identifier = "CustomTableViewCell"

    @IBOutlet var tblviewCell:UITableView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.tblviewCell.delegate = self
        self.tblviewCell.dataSource = self
        tblviewCell.isScrollEnabled = false
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {

        let heighToReturn = self.tblviewCell.contentSize.height + 20 // upper and down space
        return CGSize(width: self.tblviewCell.contentSize.width, height: heighToReturn)
    }

}

extension CustomTableViewCell:UITableViewDelegate,UITableViewDataSource{

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return 5
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: customCell.identifier) as! customCell
        cell.lblname?.text = "Vikas"
        return cell
    }


    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }

}

class customCell: UITableViewCell {

    static let identifier = "customCell"

    @IBOutlet weak var lblname :UILabel?

}
所以,若您在systemLayoutSizeFitting方法中给出tableview内容大小高度,那个么问题将得到解决


我希望这会有所帮助。

@Larme我不需要滚动子表视图。您建议如何实现我的需求?在我看来,您根本不需要在tableview中嵌套tableview。我会将“Main-Cellx”作为标题,将“Sub-Cellx”作为单元格。@FryAnEgg我是初学者,你能告诉我怎么做吗,你能给我举个例子吗?是的,我可以帮你。我可能需要几个小时才能回到我的示例代码。敬请期待。
class CustomTableViewCell: UITableViewCell {

    static let identifier = "CustomTableViewCell"

    @IBOutlet var tblviewCell:UITableView!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.tblviewCell.delegate = self
        self.tblviewCell.dataSource = self
        tblviewCell.isScrollEnabled = false
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {

        let heighToReturn = self.tblviewCell.contentSize.height + 20 // upper and down space
        return CGSize(width: self.tblviewCell.contentSize.width, height: heighToReturn)
    }

}

extension CustomTableViewCell:UITableViewDelegate,UITableViewDataSource{

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return 5
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: customCell.identifier) as! customCell
        cell.lblname?.text = "Vikas"
        return cell
    }


    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }

}

class customCell: UITableViewCell {

    static let identifier = "customCell"

    @IBOutlet weak var lblname :UILabel?

}