swift - 如何快速按下所选单元格的IndexPath
问题描述
我一直在尝试获取当前选定行的 IndexPath。我一直在使用这个功能:
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPathForSelection: IndexPath) {
print(SavedColors.SavedColorsList[indexPathForSelection.row].Name)
}
问题是它给了我刚刚取消选择的单元格的 IndexPath。如果我按下第一个单元格,什么也不会发生。如果我然后按第二个单元格,它会在第一个单元格的 indexPath 中打印“0”,依此类推。我想在按下单元格后立即获取索引路径。我该怎么做呢?
Said another way: the function is called when the selection is switched to another cell, but not when a cell is selected. 我希望用户点击,并激活该功能。相反,当您点击时,什么都不会发生,直到您点击另一个不同的单元格。我怎样才能解决这个问题?
我不知道这是否相关,但该self.tableView.deselectSelectedRow(animated: true)
功能也没有做任何事情......
谢谢
解决方案
您需要使用 didSelect 而不是 deSelect
override func tableView(_ tableView: UITableView, didSelectRowAt indexPathForSelection: IndexPath) {
print(SavedColors.SavedColorsList[indexPathForSelection.row].Name)
}
.
DidSelect 在您选择一个单元格时调用,而在您取消选择一个单元格时调用 deSelect ... 所以在编写这些函数时要小心,快乐编码 =)
推荐阅读
- jenkins - 使用 Nx 在 Jenkins 中进行 CI/CD 的最佳方式
- excel - 为什么 Range 方法 Select 第一次有效,第二次无效
- javascript - 滚动时标题中的内容移动
- css - 下拉菜单的 CSS(样式组件)问题
- javascript - 如何获取chrome中选项卡使用的实际内存?
- android - 执行“单击”时出错 - 在坐标处执行简单的单击操作
- node.js - 安装节点 js 二进制文件
- javascript - React & Axios - 为对象动态添加键和值
- java - java - 如何通过单击jbutton来更新所有空列的值,而在java中保持表中的其他数据不变?
- java - Java Hibernate -Mysql Schema-validation:列中遇到错误的列类型