gpt4 book ai didi

swift - 在 TableView 中选择和取消选择行时执行函数的问题

转载 作者:行者123 更新时间:2023-11-30 10:36:13 25 4
gpt4 key购买 nike

注意:TableView 使用 JSON 填充,结构包含值 codeNum

我需要根据是否选择或取消选择行来执行两个不同的函数,以下是我现有的选择机制:

class CheckableTableViewCell: UITableViewCell {
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.selectionStyle = .none
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
self.accessoryType = selected ? .checkmark : .none
}
}

当选择一行时,需要运行select(),当未选择行时,需要运行unselect()。在此之前,需要将行 codeNum 值分配给变量 tappedSelected:

    structure = sections[indexPath.section].items
let theStructure = structure[indexPath.row]
tappedSelected = theStructure.codeNum

如何将其实现到自定义类中?

最佳答案

您应该尝试使用 didSelectRowAt() 和 didDeselectRowAt() 方法。假设这里允许多项选择,这就是我的做法。

如果选择了单元格

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
structure = sections[indexPath.section].items
let theStructure = structure[indexPath.row]
tappedSelected = theStructure.codeNum
select(tappedSelected)
}

如果单元格未被选中

override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
structure = sections[indexPath.section].items
let theStructure = structure[indexPath.row]
tappedSelected = theStructure.codeNum
unselect(tappedSelected)
}

关于swift - 在 TableView 中选择和取消选择行时执行函数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57967586/

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