gpt4 book ai didi

ios - Swift tableView.reloadRows 删除单元格

转载 作者:可可西里 更新时间:2023-11-01 01:25:03 25 4
gpt4 key购买 nike

要求;

我有一个静态的UITableView,我试图在单击其中一个单元格时打开一个pickerview。我使用 heightForRowAt 更改大小,因此当单击 cell 时,我需要重新加载 以便更改大小。如果我使用 reloadData() 它会完美地工作(我想使用 reloadRows 所以我可以添加 animation)

问题:

当我尝试 reloadRows(at..., with: .automatic) 时,行就消失了。如果我在 viewWillAppearreloadRows 没有任何变化,该行不会消失。

代码:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath)
if colorCelllExpandedIndex != nil{
let prev = colorCelllExpandedIndex
colorCelllExpandedIndex = nil
tableView.reloadRows(at: [prev!], with: .automatic)
//tableView.reloadData()
} else {
colorCelllExpandedIndex = indexPath
colorCell.frame.size.height = CGFloat(colorCellExpandedHeight)
tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
}

}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

if indexPath == colorCelllExpandedIndex {
return CGFloat(colorCellExpandedHeight)
} else {
if indexPath.row == 1 {
return CGFloat(colorCellRegularHeight)
}
return UITableViewAutomaticDimension
}
}

屏幕图像:

before and after clicking the color button

How it looks like if I use reloadData

最佳答案

使用静态 UITableView 时,reloadRows(at:with:) 等函数似乎存在一些问题,在这个 SO question 中也有类似的问题没有决议。

但是,我能够通过实现下一个函数来更新单元格高度:

tableView.beginUpdates()
tableView.endUpdates()

因此您可以采用以下方法:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(indexPath)
if colorCelllExpandedIndex != nil{
colorCelllExpandedIndex = nil
} else {
colorCelllExpandedIndex = indexPath
}
tableView.beginUpdates()
tableView.endUpdates()

}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath == colorCelllExpandedIndex {
return CGFloat(colorCellExpandedHeight)
} else {
if indexPath.row == 0 {
return CGFloat(colorCellRegularHeight)
}
return UITableViewAutomaticDimension
}
}

这受到了 another SO question 中提供的答案之一的启发。关于在静态表格 View 中隐藏单元格。

关于ios - Swift tableView.reloadRows 删除单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42016485/

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