1

I am running into an issue where I animate the size of a simple UIView in a table cell by adjusting an autolayout constraint.

When I launch the table for the first time, everything is broken.

enter image description here

If I reload the table, it works beautifully.

enter image description here

It's a single view controller with only a few lines of code. Please download this demo project to see it in action.

Here's my code in my whole UIViewController:

import UIKit

class TableCell: UITableViewCell {
    @IBOutlet weak var constraintRedBarWidth: NSLayoutConstraint!
    @IBOutlet weak var labelTitle: UILabel!
    @IBOutlet weak var viewBarArea: UIView!
    @IBOutlet weak var viewRedBar: UIView!
}

class ViewController: UIViewController {
    @IBOutlet weak var tableView: UITableView!
    var tableData : [CGFloat] = [1, 0.90, 0.70, 0.80,0.50] //percentages

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func btnReloadTable(sender: AnyObject) {
        tableView.reloadData()
    }

}

extension ViewController: UITableViewDelegate, UITableViewDataSource {

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("TableCell") as! TableCell
        cell.labelTitle.text = "Item \(indexPath.row + 1)"
        cell.constraintRedBarWidth.constant = 1
        cell.layoutIfNeeded()
        cell.constraintRedBarWidth.constant = cell.viewBarArea.frame.width * tableData[indexPath.row]
        UIView.animateWithDuration(0.5, animations: {
            cell.layoutIfNeeded()
        })
        return cell
    }

}

viewBarArea is just the parent view of the red view and simply represents the possible max size of the bar.

Peter Hornsby
  • 4,208
  • 1
  • 25
  • 44
Travis M.
  • 10,930
  • 1
  • 56
  • 72
  • the thing is `cell.viewBarArea.frame.width` gives cell's original width...like if you used wAnyhAny and your cell width is 580 than first time it gives 580 of width....you have to use `viewWillLayoutSubviews` to get updated width as per device – Bhavin Bhadani Jan 20 '16 at 05:36
  • So in viewWIllLayoutSubviews, I should loop through my visible table cells and apply the animation? That might work but seems a bit intensive. I'll take a look – Travis M. Jan 20 '16 at 05:37
  • @El Captain, I just realized that wouldn't work because the animations would be going constantly again and again as they scroll or do anything else. – Travis M. Jan 20 '16 at 05:46

1 Answers1

3

use this line cellForRowAtIndexPath thats it

let cell = tableView.dequeueReusableCellWithIdentifier("TableCell", forIndexPath: indexPath) as! TableCell
vaibby
  • 1,255
  • 1
  • 9
  • 23
  • this is common mistakes. dont worry. read this http://stackoverflow.com/questions/25826383/when-to-use-dequeuereusablecellwithidentifier-vs-dequeuereusablecellwithidentifi or https://developer.apple.com/videos/play/wwdc2012-200/ – vaibby Jan 20 '16 at 05:47
  • 1
    Omg this is exactly what I needed! You rock! – Travis M. Jan 20 '16 at 05:56