如何在uitableviewcell中更新约束动画

问题描述 投票:0回答:2

我想在UITableView单元格中使用动画扩展视图高度。它正在工作,但动画不能按我的意愿工作。我的代码是这样的。

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()
            }

        }

    }

请检查链接屏幕:https://ibb.co/XjjXRz5

ios swift iphone uitableview uianimation
2个回答
2
投票

我相信你需要在动画调用之外设置cell.expandViewHeight.constant = 40并简单地在里面调用self.layoutIfNeeded()。像这样:

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

0
投票
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()
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.