gpt4 book ai didi

ios - viewWillAppear 不调用 didDeselectRowAt

转载 作者:行者123 更新时间:2023-11-28 11:24:09 30 4
gpt4 key购买 nike

我目前有一个 tableView,我为其创建了自定义选择和取消选择操作(淡入和淡出 View )。我遇到了一个问题,即在返回 tableView 时未调用取消选择操作。我已将必要的取消选择代码添加到我的 viewWillAppear 中,因此似乎无法确定可能出了什么问题。这个用例是否有不同的方法?

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//Deselect row on unwind
if let path = folderTableView.indexPathForSelectedRow {
folderTableView.deselectRow(at: path, animated: true)
}
}



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Select")

switch indexPath.section {
case 2:
let cell = tableView.cellForRow(at: indexPath) as! FolderTagTableViewCell
cell.folderTagSelectionBKG.alpha = 1.0
default:
let cell = tableView.cellForRow(at: indexPath) as! FolderTableViewCell
cell.folderSelectionBKG.alpha = 1.0
}
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
print("Should deselect")

switch indexPath.section {
case 2:
let cell = tableView.cellForRow(at: indexPath) as! FolderTagTableViewCell
UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseInOut, animations: {
cell.folderTagSelectionBKG.alpha = 0
})
default:
let cell = tableView.cellForRow(at: indexPath) as! FolderTableViewCell
UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseInOut, animations: {
cell.folderSelectionBKG.alpha = 0
})
}
}

最佳答案

来自 deselectRow(at:animated:) 的文档

Calling this method does not cause the delegate to receive a tableView(_:willDeselectRowAt:) or tableView(_:didDeselectRowAt:) message, nor does it send selectionDidChangeNotification notifications to observers.

Calling this method does not cause any scrolling to the deselected row.

一个解决方案是将didDeselectRowAt中的代码移动到一个额外的方法中

func deselectRowAnimated(at indexPath : IndexPath)
{
switch indexPath.section {
case 2:
let cell = tableView.cellForRow(at: indexPath) as! FolderTagTableViewCell
UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseInOut, animations: {
cell.folderTagSelectionBKG.alpha = 0
})
default:
let cell = tableView.cellForRow(at: indexPath) as! FolderTableViewCell
UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseInOut, animations: {
cell.folderSelectionBKG.alpha = 0
})
}
}

并在 viewWillAppeardidDeleselect 中调用它

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//Deselect row on unwind
if let indexPath = folderTableView.indexPathForSelectedRow {
deselectRowAnimated(at: indexPath)
}
}

...

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
deselectRowAnimated(at: indexPath)
}

选择行

关于ios - viewWillAppear 不调用 didDeselectRowAt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58849654/

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