ios - iOS:didSelectRowAt 索引路径仅适用于第二次点击
问题描述
我正在为我的 xcode 项目中的一个问题而苦苦挣扎。我在表格视图中显示自定义单元格并didSelectRowAt indexPath:
用于显示详细信息视图。它以我想要的方式工作,但奇怪的是只在第二次点击时。
我对编程还很陌生,非常感谢您的帮助。太感谢了!
我检查了我不是didDeselectRow at
偶然使用的。我还通过 stackoverflow 尝试找到解决方案,这是我能找到的最接近我的问题的解决方案。但我正在使用didSelectRow at
.
// 这是用户点击单元格时的代码:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Pop Up Variables
var id:String = tasks[indexPath.row].task_id!
var title:String = tasks[indexPath.row].task_title!
var type:String = tasks[indexPath.row].task!
var desc:String = tasks[indexPath.row].task_desc!
var action:String = "Dismiss"
present(detailVC!, animated: true, completion: {
self.detailVC!.setPopup(withTaskId: id, withTitle: title, withType: type, withDesc: desc, withAction: action)
})
}
// 这里只是一个注释,我在文档顶部设置切换到另一个视图控制器。
// 显示弹出 var detailVC:TaskDetailViewController?
override func viewDidLoad() {
super.viewDidLoad()
// Set up the task detail view controller
detailVC = storyboard?.instantiateViewController(withIdentifier: "TaskDetailVC") as! TaskDetailViewController?
detailVC?.delegate = self
detailVC?.modalPresentationStyle = .overCurrentContext
// Conform to the table view protocols
tableView.dataSource = self
tableView.delegate = self
// Set Self as delegate for model and call getTasks
// Get the Tasks from the Task Model
model.delegate = self
model.getTasks()
}
仅在我第二次点击一行后才会出现详细视图。不是第一次吗?
解决方案
几年前我有这个问题,不知道它今天仍然存在。
我通过使用 GCD 将所有逻辑封装didSelectRowAt
在主线程中来解决它。你可以看看这些:
https://stackoverflow.com/a/27907119/6642629
https://stackoverflow.com/a/26183438/6642629
您可以尝试以下方法:
斯威夫特 4
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
DispatchQueue.main.async {
var id:String = tasks[indexPath.row].task_id!
var title:String = tasks[indexPath.row].task_title!
var type:String = tasks[indexPath.row].task!
var desc:String = tasks[indexPath.row].task_desc!
var action:String = "Dismiss"
present(detailVC!, animated: true, completion: {
self.detailVC!.setPopup(withTaskId: id, withTitle: title, withType: type, withDesc: desc, withAction: action)
})
}
}
推荐阅读
- excel - vba - 获取周数
- python - 如何装饰python中的所有函数和类方法?
- mysql - 访问我的 phpmyadmin 错误 500 的问题
- robotframework - 如何验证机器人框架中的文本颜色等到页面包含?
- openedge - Progress 4gl - change font color of specific values while exporting temp table to .html file
- python - 列表和数组在存储值时有何不同?
- android - 为 android 转换旧的佳能 avi 视频
- html - 网站在运行相同浏览器的不同计算机上看起来不同
- reactjs - 如何在 creat-react-app 的 public/index.html 内的元标记中添加环境变量?
- google-sheets - 谷歌表格分层子类别扩展