动画与约束的UITableViewCell正确启动 [英] Animation with constraints in UITableViewCell launches incorrectly

查看:110
本文介绍了动画与约束的UITableViewCell正确启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了,我通过调节自动版式约束动画表格单元格一个简单的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正确启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆