【问题标题】:Increase height of tableview based on contentSize根据 contentSize 增加 tableview 的高度
【发布时间】:2017-07-18 09:54:32
【问题描述】:

我已经创建了 tableview 并且在 tableview 单元格中我已经给出了另一个子 tableview 并且还能够在两个 table view 中填充数据但不能给出 tableview 的自动增量高度,我也在 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
        }
    }

在子表格视图单元格中

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 设计

我很困惑我们能否增加主表视图单元格内子表视图的高度。我需要一些建议。

【问题讨论】:

    标签: ios uitableview swift3


    【解决方案1】:

    您可以为每个存储根据内容计算的大小的单元格设置大小数组。

    之后使用

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

    【讨论】:

    • 如何给出数组大小你可以在这里举个例子
    • 是的,我知道了,但是我很困惑如何在上面的代码中完成它
    猜你喜欢
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2016-11-20
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    相关资源
    最近更新 更多