首页 > 解决方案 > 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()


}

仅在我第二次点击一行后才会出现详细视图。不是第一次吗?

标签: iosswiftuitableviewdidselectrowatindexpath

解决方案


几年前我有这个问题,不知道它今天仍然存在。

我通过使用 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)
        })
    }
}

推荐阅读