表格视图行高改变时如何添加动画?

Ale*_*289 2 core-animation ios swift

我正在尝试制作一个 FAQ 视图控制器,我知道有一个名为“FAQView”的 pod 来制作这个 FAQ 页面,但我需要自定义,所以我自己制作。我希望我的最终结果是这样的

在此处输入图片说明

行展开时有一点动画,我需要添加该动画。我的这个常见问题的表格视图是这样的

在此处输入图片说明

当某个单元格中的行高从 80 变为 140 时,我想添加动画

这是我的代码,我的视图控制器代码:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var FAQs = [FAQ]()

    @IBOutlet weak var tableView: UITableView!

    var selectedRow : Int?
    var cellIsExpanded : Bool = false {
        didSet {
            tableView.reloadData()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        tableView.delegate = self
        getFAQ()        
    }

    func getFAQ() {

    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "FAQCell") as! TableViewCell
        cell.FAQData = FAQs[indexPath.row]
        cell.expandButton.tag = indexPath.row

        // to implement FAQCellDelegate
        cell.cellDelegate = self

        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        var height:CGFloat = CGFloat()

        if cellIsExpanded == false {
            height = 80
        } else {
            if indexPath.row == selectedRow {
                height = 160
            } else {
                height = 80
            }
        }
        return height
    }
}

extension ViewController : FAQCellDelegate {
    func didPressButton(isExpanded: Bool, tag: Int) {
        cellIsExpanded = isExpanded
        selectedRow = tag

    }
}
Run Code Online (Sandbox Code Playgroud)

我的表格视图单元格代码如下

import UIKit

protocol FAQCellDelegate : class {
    func didPressButton(isExpanded: Bool, tag: Int)
}

class TableViewCell: UITableViewCell {

    @IBOutlet weak var questionLabel: UILabel!
    @IBOutlet weak var answerLabel: UILabel!
    @IBOutlet weak var expandButton: UIButton!

    var expanded = false
    var FAQData : FAQ! {
        didSet {
            questionLabel.text = FAQData.question
            answerLabel.text = FAQData.answer
        }
    }

    weak var cellDelegate: FAQCellDelegate?

    @IBAction func expandButtonDidPressed(_ sender: Any) {

        if expanded == true {
            expanded = false
            cellDelegate?.didPressButton(isExpanded: expanded, tag: expandButton.tag)

        } else {
            expanded = true
            cellDelegate?.didPressButton(isExpanded: expanded, tag: expandButton.tag)

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

SHI*_*MIN 5

当设置“cellIsExpanded”时,不要重新加载 tableview 而是调用下面的方法。使用以下代码

在 iOS 11 中,

- (void)performBatchUpdates:(void (NS_NOESCAPE ^ _Nullable)(void))updates completion:(void (^ _Nullable)(BOOL finished))completion
Run Code Online (Sandbox Code Playgroud)

iOS 10 或以下,

- (void)beginUpdates;
- (void)endUpdates; 
Run Code Online (Sandbox Code Playgroud)