Hasmukh Dobariya
Hasmukh Dobariya

Reputation: 11

How to update constraints animation in uitableviewcell

I want to expand view height with animation in UITableView cell. It's working but animation not working as i want. My code is something like.

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

         let cell = listingTableView.dequeueReusableCell(withIdentifier: "listingTableViewCell", for: indexPath) as! listingTableViewCell

        //Charactristic expand
        let isExpand = isExpandViewArr[indexPath.row]

        if isExpand == true {

            cell.expandBtn.setImage(UIImage(named: "down-arrow"), for: .normal)
            UIView.animate(withDuration: 0.5) {
                cell.expandViewHeight.constant = 0
                self.loadViewIfNeeded()
            }
        }else{

            cell.expandBtn.setImage(UIImage(named: "up-arrow"), for: .normal)
            UIView.animate(withDuration: 0.5) {
                cell.expandViewHeight.constant = 40
                self.loadViewIfNeeded()
            }

        }

    }

Please check screen on link : https://ibb.co/XjjXRz5

Upvotes: 1

Views: 579

Answers (2)

nico
nico

Reputation: 156

I believe you need to set the cell.expandViewHeight.constant = 40 outside of the animation call and simply call self.layoutIfNeeded() inside. Like so:

cell.expandViewHeight.constant = 40
UIView.animate(withDuration: 0.5) {            
    self.layoutIfNeeded()
}

Upvotes: 3

Bhavesh Nayi
Bhavesh Nayi

Reputation: 715

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    let cell = listingTableView.dequeueReusableCell(withIdentifier: "listingTableViewCell", for: indexPath) as! listingTableViewCell

    //Charactristic expand
    let isExpand = isExpandViewArr[indexPath.row]

    if isExpand == true {

        cell.expandBtn.setImage(UIImage(named: "down-arrow"), for: .normal)
        DispatchQueue.main.async {
            self.tblView.beginUpdates()
            cell.expandViewHeight.constant = 0
            self.tblView.endUpdates()
        }
    }else{

        cell.expandBtn.setImage(UIImage(named: "down-arrow"), for: .normal)
        DispatchQueue.main.async {
            self.tblView.beginUpdates()
            cell.expandViewHeight.constant = 40
            self.tblView.endUpdates()
        }
    }
}

Upvotes: 0

Related Questions