gpt4 book ai didi

ios - 插入带有大单元格的表格单元格动画

转载 作者:行者123 更新时间:2023-11-30 12:05:48 25 4
gpt4 key购买 nike

有人可以帮我制作动画吗?我的最终目标是显示单元格的详细信息,我尝试过:固定高度,用另一个单元格替换该单元格,然后将新单元格插入到触摸的单元格下方。所有解决方案都存在与动画类似的问题。详细单元格比其他单元格大,当动画开始时,可​​以看到其他单元格下方的新单元格。

示例项目中的问题仅发生在最后两个单元格中。最后一个的结果动画有点不同,但即使使用自动,结果也是一样的。

final class ViewController: UIViewController {

@IBOutlet weak var tableView: UITableView! {
didSet {
tableView.delegate = self
tableView.dataSource = self
tableView.tableFooterView = UIView(frame: CGRect.zero)
}
}

private var array = [Bool]()
private var lastIndexPath: IndexPath?

override func viewDidLoad() {
super.viewDidLoad()

for _ in 0 ..< 10 {
array.append(false)
}
}

private func showHideBig(indexPath: IndexPath)
{
let oldIndex = self.lastIndexPath?.row ?? -1
let needInsert = oldIndex != indexPath.row
let newBigIndexPath = IndexPath(row: indexPath.row + 1, section: indexPath.section)

self.tableView.beginUpdates()

if let indexPath = self.lastIndexPath {
let row = indexPath.row + 1
self.lastIndexPath = nil

if array[row] {
array.remove(at: row)
self.tableView.deleteRows(at: [IndexPath(item: row, section: indexPath.section)], with: .top)
}
}

if needInsert {
array.insert(true, at: newBigIndexPath.row)
self.tableView.insertRows(at: [newBigIndexPath], with: .top)
self.lastIndexPath = indexPath
}

self.tableView.endUpdates()
}
}

extension ViewController: UITableViewDelegate, UITableViewDataSource
{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.array.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let value = self.array[indexPath.row]
let idetifier = !value ? "normal" : "big"
let cell = tableView.dequeueReusableCell(withIdentifier: idetifier, for: indexPath)

return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)

if !array[indexPath.row] {
showHideBig(indexPath: indexPath)
}
}
}

动画开始:/image/PcQty.png

最终结果:/image/uSljt.png

我创建了一个示例项目(插入新单元格):https://github.com/elanovasconcelos/TestTableAnimation

最佳答案

我的主要问题是表格需要页脚才能使动画看起来正确。我添加了一个比大单元格更大的单元格,之后动画就正常了。

关于ios - 插入带有大单元格的表格单元格动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46692726/

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