gpt4 book ai didi

ios - 滑动时 TableView 单元格高度

转载 作者:可可西里 更新时间:2023-10-31 23:13:50 25 4
gpt4 key购买 nike

我有一个带有可扩展单元格的 TableView,使用此代码:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let index = indexPath

if(selectedIndexPath != nil) {
if(index == selectedIndexPath) {
return 90
}
else {
return 60
}
}
else {
return 60
}
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch selectedIndexPath {
case nil:
selectedIndexPath = indexPath
default:
if selectedIndexPath! == indexPath {
selectedIndexPath = nil
}
else {
selectedIndexPath = indexPath
}
}
tableView.reloadRows(at: [indexPath], with: .automatic)
}

现在,我还想为每个单元格添加滑动手势,使用创建的代码 here .

func tableView(tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let share = UITableViewRowAction(style: .Normal, title: "Share") { action, index in
print("share button tapped")
}
return [share]
}

一切都很好,除了滑动时单元格的高度。

  1. 滑动的单元格,有时,有很大的高度(即使它们不是选中)
  2. 滑动的单元格有时高度很小(即使它们是选中)。

我认为这可能是因为最后选择的单元格高度。

我尝试在 editActionsForRowAt 中使用 selectedIndexPath = indexPath 进行修复,但问题仍然存在。

/image/iQCEe.png

编辑:我注意到,如果 heightForRowAt 始终返回 60,问题仍然存在。

最佳答案

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let index = indexPath

if(selectedIndexPath != nil) {
if(index == selectedIndexPath) {
return 90
}
else {
return 60
}
}
else {
return 60
}
}

它对我有用,你也可以检查是否有任何 tableview 单元格的所有约束都正确。或者你可以只使用任何一种 tableview 高度方法来获得更好的结果。

关于ios - 滑动时 TableView 单元格高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40946889/

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