UITableViewCell中的约束animation启动不正确

我遇到了一个问题,我通过调整自动布局约束来在表格单元格中设置一个简单UIView的大小。

当我第一次启动桌子时,一切都被打破了。

在这里输入图像说明

如果我重新加载表,它运作的很好。

在这里输入图像说明

这是一个只有几行代码的单一视图控制器。 请下载这个演示项目来看看它的行动。

这是我的整个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只是红色视图的父视图,只是表示可能的最大大小。

使用这行cellForRowAtIndexPath这就是它

 let cell = tableView.dequeueReusableCellWithIdentifier("TableCell", forIndexPath: indexPath) as! TableCell