Alexey K
Alexey K

Reputation: 6723

Animation inside last cell of tableview

I have table view. Inside cell I have method that animates view inside cell.

func animateCell() {
    UIView.animate(withDuration: 0.4, animations: { [weak self] in
        self?.quantityBackround.transform = CGAffineTransform(scaleX: 25, y: 25)
    }) { [weak self]  _ in
        UIView.animate(withDuration: 0.1) {
            self?.quantityBackround.transform = CGAffineTransform.identity
        }
    }
}

Also I have prepareForReuse()

    override func prepareForReuse() {
    quantityBackround.transform = CGAffineTransform.identity
   }

The animation must work only for last cell when array of datasource changes and I do this in property observer like this (fires when something is being added to array)

guard let cell = checkTableView.cellForRow(at: IndexPath(row: viewModel.checkManager.check.count - 1, section: 0)) as? CheckItemTableViewCell else { return }
cell.animateCell()

All of this works fine.

One problem, is that I encounter is that when tableView is reloaded, all background views in all cells expand from zero size to its initial. Last cell animates ok.

I think that i miss something in prepareForReuse and because of this i see this glitch of inreasing from zero to initial size.

How to fix it ?

Upvotes: 1

Views: 147

Answers (1)

Reinier Melian
Reinier Melian

Reputation: 20804

You need to implement this method of UITableViewDelegate

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    //here check if your this cell is the last one, something like this
    if (indexPath.row == yourDataSourceArray.count - 1)
     {
         if let customCell = cell as? CheckItemTableViewCell{
           customCell.animateCell()
          }
     }
}

Hope this helps

Upvotes: 1

Related Questions