【问题标题】:How to animate UITableViewCell using Swift?如何使用 Swift 为 UITableViewCell 设置动画?
【发布时间】:2016-12-22 14:06:45
【问题描述】:

我有一个 UITableView,在 tableViewCell 里面我有一个 UICollectionView。

我的要求是在点击第一个 tableView 单元格的按钮时,我必须为第二个 tableViewCell 设置动画。

下面是我的代码:-

 //Cell For Row at indexPath
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if (0 == indexPath.section) {
        let cell = tableView.dequeueReusableCell(withIdentifier: "FirstRowCell") as! FirstRowCell
        cell.btnReview.addTarget(self, action: #selector(GotoReview), for: UIControlEvents.touchUpInside)
        cell.btnMyProduct.addTarget(self, action: #selector(AnimateCollectionView), for: UIControlEvents.touchUpInside)
        //cell.

        return cell
    } else if ( 1 == indexPath.section) {
        let identifier = "TableCollectionCell"
        var tableCollectionCell = tableView.dequeueReusableCell(withIdentifier: identifier) as? TableCollectionCell

        if(tableCollectionCell == nil) {
            let nib:Array = Bundle.main.loadNibNamed("TableCollectionCell", owner: self, options: nil)!
            tableCollectionCell = nib[0] as? TableCollectionCell
            tableCollectionCell?.delegate = self
        }

        return tableCollectionCell!
    } else {
        let identifier = "BrandImagesTableCell"
        var brandImagesTableCell = tableView.dequeueReusableCell(withIdentifier: identifier)

        if(brandImagesTableCell == nil) {
            let nib:Array = Bundle.main.loadNibNamed("BrandImagesTableCell", owner: self, options: nil)!
            brandImagesTableCell = nib[0] as? BrandImagesTableCell
          }

        //brandImagesTableCell.
        return brandImagesTableCell!
    }
} 

在我的代码中你可以看到:

        if (0 == indexPath.section)  

我有一个按钮目标 (#selector(AnimateCollectionView))。
我想为 (1 == indexPath.section) 的 tableCollectionCell 设置动画。

查看我的AnimateCollectionView 方法:-

func AnimateCollectionView() {
    let identifier = "TableCollectionCell"
    var tableCollectionCell = tableView.dequeueReusableCell(withIdentifier: identifier) as? TableCollectionCell

    if(tableCollectionCell == nil) {
        let nib:Array = Bundle.main.loadNibNamed("TableCollectionCell", owner: self, options: nil)!
        tableCollectionCell = nib[0] as? TableCollectionCell
        tableCollectionCell?.delegate = self
    }

    tableCollectionCell?.alpha = 0

    UIView.animate(withDuration: 1.50, animations: {
        //self.view.layoutIfNeeded()
        tableCollectionCell?.alpha = 1
    })
}  

【问题讨论】:

  • 你想让第二个单元格如何动画?旋转?规模 ?向右移动?向左移动?
  • @aircraft 我想旋转。谢谢
  • @Rob 感谢您的回复。你能用代码告诉我吗?谢谢

标签: ios swift uitableview animation


【解决方案1】:

如果你想动画它的变化,你可以改变一些状态变量,调用tableView.reloadRows(at:with:),然后让你的cellForRowAt检查这些状态变量以了解最终的单元格应该是什么样子(即它是否是“之前”或“之后”单元配置)。

例如,这里有一个示例,其中有两个单元格,将第二个单元格从红色单元格切换到蓝色单元格。

class ViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(UINib(nibName: "RedCell", bundle: nil), forCellReuseIdentifier: "RedCell")
        tableView.register(UINib(nibName: "BlueCell", bundle: nil), forCellReuseIdentifier: "BlueCell")
    }

    @IBAction func didTapButton(_ sender: UIButton) {
        isRedCell = !isRedCell
        tableView.reloadRows(at: [IndexPath(row: 1, section: 0)], with: .fade)
    }

    var isRedCell = true

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ButtonCell", for: indexPath)
            return cell
        } else if indexPath.row == 1 {
            if isRedCell {
                let cell = tableView.dequeueReusableCell(withIdentifier: "RedCell", for: indexPath)
                return cell
            } else {
                let cell = tableView.dequeueReusableCell(withIdentifier: "BlueCell", for: indexPath)
                return cell
            }
        }
        fatalError("There are only two rows in this demo")
    }

}

现在,假设您确实需要使用 NIB 而不是单元原型,我将使用上面的 tableview 注册 NIB,而不是让 cellForRow 必须自己手动实例化它们。同样,对于带有按钮的单元格,我只是在情节提要中使用了一个原型单元格,并将按钮直接连接到我的 @IBOutlet,从而完全避免了该单元格的 NIB。

但所有这些都与您手头的主要问题无关:您可以随心所欲地创建单元格。但希望这说明了基本思想,即在点击按钮时,我并没有尝试直接加载任何单元格,但我只是更新了一些状态变量,cellForRow 将使用它来知道要加载哪个单元格,然后告诉tableView 为 IndexPath 的重新加载设置动画。

顺便说一句,我从您的示例中假设第二行的两个不同的潜在单元格需要不同的 NIB。但如果你不这样做,那就更容易了(只使用一个 NIB 和一个单元格标识符),但想法是一样的,只需更新你的状态变量并用动画重新加载第二行。

【讨论】:

  • 其实一共有3个部分所以我把这些条件。谢谢让我试试这个。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-19
  • 2018-02-10
  • 1970-01-01
  • 1970-01-01
  • 2015-06-05
  • 1970-01-01
相关资源
最近更新 更多