gpt4 book ai didi

ios - UITableViewCell 在显示期间具有不一致的 UIView 动画

转载 作者:行者123 更新时间:2023-11-29 01:28:17 25 4
gpt4 key购买 nike

我正在开发一个基于 Swift 2.0(和自动布局)的项目。我正在尝试构建一个带有简单扩展器控件的 View Controller 。用户单击标题位,它会展开以显示一个选项表。

它有效,但最初显示时有一点奇怪。用户第一次单击标题时,它会按预期展开,但表格内容会从左侧略微滑入。在随后的点击中,它会在没有这种额外动画的情况下展开。

如果可以的话,我想消除这种轻微的“滑入”。但是我对此还很陌生,所以我不完全明白我做错了什么。事实证明,搜索 Google 和 SO 是令人沮丧的。我认为我不知道正确的术语来解释正在发生的事情。

我提供了示例代码来说明我在做什么。我还包含了一个动画,这样您就可以看到正在发生的事情。谢谢!

代码:

import UIKit

class ViewController: UIViewController, UITableViewDataSource {
@IBOutlet weak var tableViewHeight: NSLayoutConstraint!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableViewHeight.constant = 0
}

@IBAction func openTable(sender: AnyObject) {
if tableViewHeight.constant == 0 {
tableViewHeight.constant = 220
} else {
tableViewHeight.constant = 0
}

UIView.animateWithDuration(0.3, animations: {
self.view.layoutIfNeeded()
})
}

// MARK: tableView stuff
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 3 }

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = "Cell \(indexPath.item)"
return cell
}

}

图片:

UITableView errant slide in

最佳答案

您应该尝试在显示前制作单元格布局,避免出现这种奇怪的效果。

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
let cell = // get your cell for indexPath here
cell.layoutIfNeeded()
}

关于ios - UITableViewCell 在显示期间具有不一致的 UIView 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33763533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com