UiTableview swift 2中的Fixe size子视图

时间:2016-05-02 20:33:34

标签: uitableview swift2 subview

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

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

        return 150.0
    }

但是值是固定的,所以滚动条出现在我的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!
    }
}

我该如何解决?

1 个答案:

答案 0 :(得分:0)

尝试

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