2016-05-02 27 views
1

我在ViewController中創建了一個Uitableview,我想動態設置每個包含子視圖的單元格的大小。我嘗試了很多工藝中,唯一一個使用此功能:UiTableview中的Fixe大小子視圖swift 2

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

     return 150.0 
    } 

但其價值定勢使滾動條出現在我的tableview細胞,我不希望這樣,我希望看到所有的子視圖沒有滾動條,這是我對包含實現代碼如下的視圖控制器代碼:

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! 
    } 
} 

我怎麼固定呢?

回答

0

嘗試

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

我認爲這是正確的方式,但它崩潰這一行,沒有錯誤消息...... – fandro

+0

你試過像這樣'tableView.cellForRowAtIndexPath(indexPath)?.框@fandro .size.height'? – 4oby

+0

是的,這是糾正Xcode – fandro