2017-07-18 104 views
1

我已经创建了tableview,并在tableview单元格中给出了另一个子tableview,并且能够在两个表视图中填充数据,但不能给出tableview的自动增量高度,我给出了约束高度也在tableview单元格但它不增加子tableview的高度,当我给主tableview中的子tableview约束高度它给我错误所以如何增加子tableview的高度?基于contentSize增加tableview的高度

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if tableView == tableview1{ 
     return fees.count 
     }else{ 
     return descriptionFe.count 
     } 

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

     if tableView == tableview1{ 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FeesTableViewCell 
     let getdata = fees[indexPath.row] 
     cell.billno_txt.text = getdata.Billno 
      let date = getdata.recivedDate 
      let dateFormatter = DateFormatter() 
      dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss" 
      let dateFromString : NSDate = dateFormatter.date(from: date)! as NSDate 
      dateFormatter.dateFormat = "dd-MM-yyyy" 
      let datenew = dateFormatter.string(from: dateFromString as Date) 
      cell.received_date_txt.text = datenew 


      cell.status_txt.text = getdata.status 
      cell.total_amount_txt.text = getdata.AmountPaid 

      let date1 = getdata.recivedDate 
      let dateFormatter1 = DateFormatter() 
      dateFormatter1.dateFormat = "yyyy-MM-dd'T'HH:mm:ss" 
      let dateFromString1 : NSDate = dateFormatter1.date(from: date1)! as NSDate 
      dateFormatter1.dateFormat = "dd-MM-yyyy" 
      let datenew1 = dateFormatter1.string(from: dateFromString1 as Date) 
      cell.date_txt.text = datenew1 




     return cell 

     } 
     else{ 
      let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! FeesTableViewCell 
//   

     cell.innertableviewheight.constant = tableView.contentSize.height 



      cell.inner_txt1.text = fees[indexPath.row].Billno 

      cell.inner_txt2.text = fees[indexPath.row].AmountPaid 

      return cell 
     } 
    } 
子的tableview细胞

class FeesTableViewCell: UITableViewCell{ 
    @IBOutlet weak var billno_txt: UILabel! 
    @IBOutlet weak var date_txt: UILabel! 


    @IBOutlet weak var total_amount_txt: UILabel! 

    @IBOutlet weak var status_txt: UILabel! 

    @IBOutlet weak var received_date_txt: UILabel! 


    @IBOutlet weak var innertableviewheight: NSLayoutConstraint! 



    @IBOutlet weak var inner_txt1: UILabel! 

    @IBOutlet weak var inner_txt2: UILabel! 
    @IBOutlet weak var inner_tableview: UITableView! 

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


    } 
    override init(style: UITableViewCellStyle, reuseIdentifier: String!) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 
     innertableviewheight.constant = inner_tableview.contentSize.height 

      } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder)! 
//  fatalError("init(coder:) has not been implemented") 
      } 


} 

这里是我的UI设计 enter image description here

我'混淆,我们可以增加主表视图细胞内子的tableview的高度。我需要一些建议。

回答

0

您可以根据内容计算大小的每个单元格的大小数组。在使用

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
{ 
    // return your sizeArray[indexPath.row] 
} 

如您行表项中取20高度

让想你的第一排只有1项,你可以存储20 你的第二排只有3个项目后就可以

存储60等

let sizeArray = [20,60](); 

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 
{ 
    return sizeArray[indexPath.row] 
} 
+0

如何给数组大小您能不能给这里的例子 –

+0

更新我的回答如与代码 –

+0

雅我得到这但是如何在上面的代码中完成,我很困惑 –