问题描述
我遇到了,我通过调节自动版式约束动画表格单元格一个简单的UIView的大小的问题。
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.
如果我重装表,它精美的作品。
If I reload the table, it works beautifully.
这是只有几行code的单一视图控制器。请下载此看到它在行动。
It's a single view controller with only a few lines of code. Please download this demo project to see it in action.
下面是我的code在我的整个UIViewController中:
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只是红色视图的父视图和简单地重新presents酒吧的可能最大大小。
viewBarArea is just the parent view of the red view and simply represents the possible max size of the bar.
推荐答案
使用此行的cellForRowAtIndexPath完蛋了。
use this line cellForRowAtIndexPath thats it
let cell = tableView.dequeueReusableCellWithIdentifier("TableCell", forIndexPath: indexPath) as! TableCell
这篇关于动画与约束的UITableViewCell正确启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!