修复UiTableview swift 2中的大小子视图

修复UiTableview swift 2中的大小子视图,uitableview,swift2,subview,Uitableview,Swift2,Subview,我在ViewController中创建了Uitableview,并希望动态设置包含子视图的每个单元格的大小。我尝试了很多技术,唯一的一个就是使用这个功能: func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return 150.0 } 但是该值是fixe,因此在我的tableview单元格中会出现一个滚动条,我不

我在ViewController中创建了Uitableview,并希望动态设置包含子视图的每个单元格的大小。我尝试了很多技术,唯一的一个就是使用这个功能:

func  tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        return 150.0
    }
但是该值是fixe,因此在我的tableview单元格中会出现一个滚动条,我不希望出现这种情况,我希望看到没有滚动条的所有子视图,以下是我的包含tableview的ViewController代码:

import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 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.
    }


    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 3;
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1;
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell:CustomCell? = tableView.dequeueReusableCellWithIdentifier("Cell") as?  CustomCell
        if(cell == nil)
        {
            cell = CustomCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
        }
        cell?.dataArr = ["Test 1","Test 2","Test 3","Test 4","Test 5"]

        return cell!
    }

   func  tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

        return 150.0
    }
}
这是自定义单元格:

import UIKit

class CustomCell: UITableViewCell,UITableViewDataSource,UITableViewDelegate {


    var dataArr:[String] = []
    var subMenuTable:UITableView?
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style , reuseIdentifier: reuseIdentifier)
        setUpTable()
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")

    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        setUpTable()
    }

    func setUpTable()
    {
        subMenuTable = UITableView()
        subMenuTable?.delegate = self
        subMenuTable?.dataSource = self
        self.addSubview(subMenuTable!)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        subMenuTable?.frame = CGRectMake(0.2, 0.3, self.bounds.size.width-5, self.bounds.size.height-5)
    }

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

        // Configure the view for the selected state
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArr.count
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("cellID")

        if(cell == nil){
            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cellID")
        }
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 160.0
        cell?.textLabel?.text = dataArr[indexPath.row]

        return cell!
    }
}
我怎样才能解决这个问题呢?

试试看

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return tableView.cellForRowAtIndexPath(indexPath)?.contentView.frame.size.height
    }
试一试


@fandro你有没有这样尝试过
tableView.cellforrowatinexpath(indexPath)?.frame.size.height
?@fandro你有没有这样尝试过
tableView.cellforrowatinexpath(indexPath)?.frame.size.height