2

I have a view controller in which i have used table view controller in it. In my cell there is a button on which when user click the cell size should come to 550 and when click again it should come back to its original height. I have tried bit code after searching for it but it isn't working is their any solution that can work for me?. My code is bit this,

var indexOfCellToExpand: Int!

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  if indexPath.row == indexOfCellToExpand {
    return 170 + expandedLabel.frame.height - 38
  }
  return 170
}
mugx
  • 9,869
  • 3
  • 43
  • 55
Hamza Imran
  • 55
  • 1
  • 7

2 Answers2

3

Use Auto layout for Expand-collapse Cell, Attaching Demo for that case

link:https://www.dropbox.com/s/ieltq0honml35l8/TAbleDemo.zip?dl=0.

set cell button height constraint and update constraint on Value on select- deselect event and just reload data

in main viewcontroller code

self.tblView.estimatedRowHeight = 100
self.tblView.rowHeight = UITableViewAutomaticDimension

  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TblCell", for: indexPath) as! TblCell

        cell.btnExpandCollepse.addTarget(self, action: #selector(ViewController.expandCollapse(sender:)), for: .touchUpInside)
        return cell

    }

 @objc func expandCollapse(sender:UIButton) {

        self.tblView.reloadData()
    }

Code in Cell Class

@IBOutlet var btnExpandCollepse: UIButton!
@IBOutlet var constraintBtnHeight: NSLayoutConstraint!

@IBAction func onExpandCollepse(_ sender: UIButton) {

        sender.isSelected = !sender.isSelected

        if !sender.isSelected{

            self.constraintBtnHeight.constant = 50
        }else{
            self.constraintBtnHeight.constant = 500
        }
    }

for constraint check below image http://prntscr.com/hur8ym

Updated Demo for custom Content height of Lable with Autoresizing Cell https://www.dropbox.com/s/o742kflg5yeofb8/TAbleDemo%202.zip?dl=0

https://www.dropbox.com/s/o742kflg5yeofb8/TAbleDemo%202.zip?dl=0

2

Swift 4.x

fileprivate var expandedIndexSet = Set<IndexPath>()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if expandedIndexSet.contains(indexPath) {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CELL_EXPANDED", for: indexPath) as! CellExpanded
        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CELL_COLLAPSED", for: indexPath) as! CellCollapsed
        return cell
    }
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: false)
    if expandedIndexSet.contains(indexPath) {
        expandedIndexSet.remove(indexPath)
    } else {
        expandedIndexSet.insert(indexPath)
    }
    tableView.reloadRows(at: [indexPath], with: .fade)
}
SPatel
  • 4,768
  • 4
  • 32
  • 51
  • have to place two cells with eachother? @SPatel – Hamza Imran Jan 01 '18 at 10:46
  • While you could to this you don't really need to. You can just use dynamically sized cells and then adjust the size of the content of the cell and it will change it's height. This allows for better animations. Depending on what you want in the cell a `UIStackView` is probably the way to go. – Upholder Of Truth Jan 01 '18 at 10:47
  • @HamzaImran Just conditionally return cell type in tableView(:UITableView, cellForRowAt: ) method and reload cell on click event simple – SPatel Jan 01 '18 at 10:56
  • And also use self size tableview if possible – SPatel Jan 01 '18 at 10:57