0

someone could help me with the animation? My final objetive is to show details of a cell, I tried: fixed height, replace the cell with another and insert the new cell bellow the touched cell. All solutions have similar problems with the animation. The detail cell is bigger than the other cells and when the animation starts it's possible to see the new cell below other cell.

The problem in the example project only happen in the last two cells. The result animation in the last one is a little different, but even with automatic, the result is the same.

final class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.delegate = self
            tableView.dataSource = self
            tableView.tableFooterView = UIView(frame: CGRect.zero)
        }
    }

    private var array = [Bool]()
    private var lastIndexPath: IndexPath?

    override func viewDidLoad() {
        super.viewDidLoad()

        for _ in 0 ..< 10 {
            array.append(false)
        }
    }

    private func showHideBig(indexPath: IndexPath)
    {
        let oldIndex = self.lastIndexPath?.row ?? -1
        let needInsert = oldIndex != indexPath.row
        let newBigIndexPath = IndexPath(row: indexPath.row + 1, section: indexPath.section)

        self.tableView.beginUpdates()

        if let indexPath = self.lastIndexPath {
            let row = indexPath.row + 1
            self.lastIndexPath = nil

            if array[row] {
                array.remove(at: row)
                self.tableView.deleteRows(at: [IndexPath(item: row, section: indexPath.section)], with: .top)
            }
        }

        if needInsert {
            array.insert(true, at: newBigIndexPath.row)
            self.tableView.insertRows(at: [newBigIndexPath], with: .top)
            self.lastIndexPath = indexPath
        }

        self.tableView.endUpdates()
    }
}

extension ViewController: UITableViewDelegate, UITableViewDataSource
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.array.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let value = self.array[indexPath.row]
        let idetifier = !value ? "normal" : "big"
        let cell = tableView.dequeueReusableCell(withIdentifier: idetifier, for: indexPath)

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        if !array[indexPath.row] {
            showHideBig(indexPath: indexPath)
        }
    }
}

Start of the animation: https://i.stack.imgur.com/PcQty.png

The final result: https://i.stack.imgur.com/uSljt.png

I created an example project (inserting the new cell): https://github.com/elanovasconcelos/TestTableAnimation

3stud1ant3
  • 3,586
  • 2
  • 12
  • 15

1 Answers1

0

My main problem was that the table needs a footer for the animation to look right. I added one bigger then the big cell, after that the animation was ok.