gpt4 book ai didi

ios - TableView 行选择更改时的操作

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

我实现了一个 TableView 。该列表显示正常,但我想检测行选择何时更改。我正在尝试在更改行选择时执行某个操作,例如 print("row selection has changed ")

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableArray.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "exploreCell", for: indexPath)
cell.textLabel?.text = tableArray[indexPath.row]
return cell
}

我希望问题很清楚。

最佳答案

您可以从这 2 个委托(delegate)方法中选择观察者状态。因此,当您选择任何单元格时,它将调用 didSelectRowAt 委托(delegate)方法,如果您再次点击选定的单元格,它将调用 didDeselectRowAt

如果您希望用户选择多个单元格,请确保将 tableView 选择属性设置为多选

tableView.allowsMultipleSelection = true

Swift 委托(delegate)方法。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//Your code here
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
//Your code here
}

关于ios - TableView 行选择更改时的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56896469/

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