簡體   English   中英

根據contentSize增加tableview的高度

[英]Increase height of tableview based on contentSize

我創建了tableview,並在tableview單元格中給了另一個子tableview,並且還能夠在兩個tableview中填充數據,但是無法給出tableview的自動增量高度,我在tableview單元格中也給出了約束高度,但是它沒有增加子tableview的高度,當我在主tableview中給sub tableview約束高度時,它給我錯誤那么如何增加sub 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設計 在此處輸入圖片說明

我很困惑我們可以增加主表視圖單元格內子表視圖的高度。我需要一些建議。

您可以為每個單元格包含一個大小數組,其中根據內容存儲計算出的大小。

在使用之后

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

例如,您的1列條目的高度為20

假設您的第一行只有1個可以存儲20個項目,第二行只有3個可以存儲60個項目,依此類推

let sizeArray = [20,60]();

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM