ios - 如何使用 Swift 为 UITableViewCell 设置动画?

标签 ios swift uitableview animation

我有一个 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
    })
}  

最佳答案

如果你想动画化它的变化,你可以只改变一些状态变量,调用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 而不是单元格原型(prototype),我会像上面那样在 tableview 中注册 NIB,而不是让 cellForRow 必须自己手动实例化它们。同样,对于带有按钮的单元格,我只是在 Storyboard中使用了一个原型(prototype)单元格,并将按钮直接连接到我的 @IBOutlet,完全避免了该单元格的 NIB。

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

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

关于ios - 如何使用 Swift 为 UITableViewCell 设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41285009/

相关文章:

swift - 使用 RxSwift 将 Alamofire 请求绑定(bind)到 TableView

IOS同步音乐流媒体

ios - RxSwift - onError 发出两次

objective-c - 如何使用 Swift 调用需要 (int *) 类型参数的 Objective-C 方法?

ios - reloadData 不刷新屏幕

iphone - 为什么我收到错误 "[UIViewController tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x4e38c50'“?

ios - 重新生成 iOS APNS 证书;设备 token 受影响?

ios - 跳跃时旋转 - iOS游戏

ios - Swift 中的泛型和 ViewModel

ios - swift 8 iOS 数据在动态添加数据时在 ScrollView 中被裁剪