ios - 使 UItableview 单元格高度等于它的子 UItableview

标签 ios iphone uitableview swift3

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

here

最佳答案

首先看到我的 ViewController,它有一个 tableview(tblview) 和 UITableViewCell(CustomTableViewCell),

class ViewController: UIViewController {

    @IBOutlet var tblview:UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tblview.delegate = self
        self.tblview.dataSource = self

        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

extension ViewController: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: CustomTableViewCell.identifier) as! CustomTableViewCell
        return cell
    }


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

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

然后查看我的 CustomTableViewCell,它在一个单元格中有一个表格 View 和一个标签。看,

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 内容大小高度,那么问题就会解决。

希望对您有所帮助。

关于ios - 使 UItableview 单元格高度等于它的子 UItableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46328733/

相关文章:

ios - 适合 iOS 8 Today Extensions 中的宽度

objective-c - 如何在 iOS 中使用 UISearchBar 从 sqlite 中搜索数据?

ios - 正则表达式不起作用并且总是检测匹配

objective-c - 自定义 UITableViewCell + resignFirstResponder

ios - 我想更新我的 tableview 静态单元格,我该如何更新它?

objective-c - 自定义 UIButton 圆角

iphone - 在不同的 UIViewControllers 中重用自定义 UITableView

java - 如何从 native iOS 和/或 Android 应用程序触发转换像素/跟踪像素?

objective-c - 当 iphone 中有来电和去电时,我们可以触发事件吗?

ios - Swift 中的自定义 UITableViewCell 委托(delegate)模式