Question: Sub UITableView is not updating height automatically in Swift

Question

Sub UITableView is not updating height automatically in Swift

Answers 0
Added at 2016-12-27 15:12
Tags
Question

I have a Main UITableView. I have created a custom UITableviewCell. I have set up another Sub UITableview to be displayed within the Custom Cell.

The Main Tableview has expandable and collapsing sections. When any section expands it shows the Custom Cell views. The Custom Cell also has another UITableView implemented with the same expanding and collapsing sections and this is creating a problem as the height of the Main Tableview and the Custom Cell is not updating itself to accommodate these subcells. So this tends to hide the Sub Tableview Cells.

Please advise what can be done here

Pasting the code

//Main View Controller

override func viewDidLoad() {
        super.viewDidLoad()
 self.ruleArray=arr.objectAtIndex(array) as! NSMutableArray

        self.isExpandable=[]

        for _ in 1...self.ruleArray.count {
            self.isExpandable.append(false)
        }

        self.mainTableView.hidden=false
        self.mainTableView.userInteractionEnabled=true
        self.mainTableView.setNeedsLayout()
        self.mainTableView.layoutIfNeeded()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int
    {
        return ruleArray.count
    }
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let Identifier: String = "section"
        let cell = tableView.dequeueReusableCellWithIdentifier(Identifier)as! MainSectionMatchTableViewCell
        cell.groupLabel.text="Ground "+String(section+1)

        let singleTap = UITapGestureRecognizer(target: self, action: #selector(selectionSection(_:)))
        singleTap.numberOfTapsRequired = 1

        cell.userInteractionEnabled=true
        cell.groupView.addGestureRecognizer(singleTap)
        cell.groupView.tag=section
        return cell
    }
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 80
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if isExpandable[section]==true
        {

        return 1
        }
        else
        {
            return 0
        }
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let Identifier: String = "custom"
        let cell = tableView.dequeueReusableCellWithIdentifier(Identifier, forIndexPath: indexPath)as! CustomCellTableViewCell

        rowHeight=cell.subTableHeightConstraint.constant
        print("row",rowHeight)
        return cell
    }

    func  tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if isExpandable[indexPath.section]==true
        {
        print("row2",rowHeight)
        return rowHeight
        }
        else
        {
            return 0
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func selectionSection(sender:UITapGestureRecognizer)
    {
        rowHeight=CGFloat()
                headerView=(sender.view?.tag)!
                print(headerView)
        self.ruleArray1=self.ruleArray.objectAtIndex(headerView) as! NSMutableArray
        Constant.sharedInstance.matchArray=self.ruleArray1
                if self.isExpandable[headerView]==false
                {
                    isExpandable[headerView]=true
                    self.groupTableView.reloadData()
                }
                else if self.isExpandable[headerView]==true
                {
                    isExpandable[headerView]=false
                    self.groupTableView.reloadData()
                }
           }


//Custom Cell Class

override func awakeFromNib() {
        super.awakeFromNib()
self.subMenuTableView.delegate=self
        self.subMenuTableView.dataSource=self
        self.subMenuTableView.setNeedsLayout()
        self.subMenuTableView.layoutIfNeeded()
        self.subTableHeightConstraint.constant=0
        self.subMenuArray=Constant.sharedInstance.matchArray
        print("match",self.subMenuArray)
                self.isExpandable=[]
        roundCount=self.subMenuArray.count
        print("t",self.subMenuTableView.frame.size.height)

        for _ in 1...self.subMenuArray.count {
            self.isExpandable.append(false)
self.subTableHeightConstraint.constant+=80
        }

//        self.subMenuTableView.reloadData()

    }



    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int 
   {
             return self.subMenuArray.count
    }
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let Identifier: String = "subsection"
        let cell = tableView.dequeueReusableCellWithIdentifier(Identifier)as! SubSectionTableViewCell

        let singleTap = UITapGestureRecognizer(target: self, action: #selector(selectionSection(_:)))
        singleTap.numberOfTapsRequired = 1

        cell.userInteractionEnabled=true
        cell.sectionView.addGestureRecognizer(singleTap)
        cell.sectionView.tag=section
        return cell
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        if isExpandable[section]==true
        {

            return self.ruleArray.count
        }
        else
        {
            return 0
        }

    }

    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
    {
        return 80
            }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
    {
        if isExpandable[indexPath.section]==true
        {
        self.subTableHeightConstraint.constant+=178

            return 178
        }
        else
        {
            return 0
        }

    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let Identifier: String = "cell"
        let cell = tableView.dequeueReusableCellWithIdentifier(Identifier, forIndexPath: indexPath)as! SubCellTableViewCell
        print("3",self.subTableHeightConstraint.constant
         return cell

    }
    func tableView(tableView: UITableView, shouldShowMenuForRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }
    func selectionSection(sender:UITapGestureRecognizer)
    {
        headerView=(sender.view?.tag)!
        print(headerView)
        self.ruleArray=self.matchArray.objectAtIndex(headerView) as! NSMutableArray
        ListCount=self.ruleArray.count
        if self.isExpandable[headerView]==false
        {
            isExpandable[headerView]=true
            self.subMenuTableView.reloadData()
        }
        else if self.isExpandable[headerView]==true
        {
            isExpandable[headerView]=false
            self.subMenuTableView.reloadData()
        }

    }
Answers to

Sub UITableView is not updating height automatically in Swift

Source Show
◀ Wstecz