Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift didSelectRowAtIndexpath未响应单击_Swift_Uitableview - Fatal编程技术网

Swift didSelectRowAtIndexpath未响应单击

Swift didSelectRowAtIndexpath未响应单击,swift,uitableview,Swift,Uitableview,我正在为高中生制作一个复习应用程序。在这个视图中,用户应该能够为某个主题选择他们想要学习的单元,并且所选单元的索引路径将存储在int数组中,并传递到下一个视图以显示图像。不幸的是,由于某种原因,didSelectRowAt方法没有响应 委托和数据源设置正确,我知道在函数中编写print语句时没有调用它,当我单击任何一个表单元格时,控制台中没有打印任何内容 var units = [String]() var selectedRows = [Int]() override func viewDi

我正在为高中生制作一个复习应用程序。在这个视图中,用户应该能够为某个主题选择他们想要学习的单元,并且所选单元的索引路径将存储在int数组中,并传递到下一个视图以显示图像。不幸的是,由于某种原因,
didSelectRowAt
方法没有响应

委托和数据源设置正确,我知道在函数中编写print语句时没有调用它,当我单击任何一个表单元格时,控制台中没有打印任何内容

var units = [String]()
var selectedRows = [Int]()

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
}

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

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

func tableView(tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    selectedRows.append(indexPath.row)
    print(selectedRows)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destination = segue.destination as! ImageView
    destination.folderNames = selectedRows
}
我期望所选单元格的行索引的整数数组。但现在它只是空的。

您应该使用

而不是你现有的

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath)
尝试并分享您的结果。

  • 首先,您的didSectRow已去除润滑脂,请使用 低于一

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    
  • 其次,您必须将TableView委托设置为下面的Self-like

    tableView.delegate = self
    

如果您在问题中显示的代码是准确的,则
didSelectRowAt
方法的签名是不正确的。应该是:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

请注意第一个参数的前导“\u1”。如果您的类是UITableViewController,则还需要添加一个
override
关键字

如果您的代码已经使用此签名,请更新您的问题以反映这一点

此外,此前导代码也不正确。请将其更新为您实际使用的内容


var units=String var selectedRows=Int

是否可以检查是否调用了其他委托方法?是的,单元格设置正确,并通过prepareforsegue将空数组传递到下一个视图@KeyurTailor@TimmyMi你好,segue,你能给我们看看密码吗?@MuhammadWaqasBhati对不起,我不熟悉堆栈溢出,如何在评论区共享代码?@TimmyMi edit您的答案会很好。仍然没有打印任何内容,我想问题是当我单击我的单元格时,该方法没有被调用。@TimmyMi好的,当然,如果您遇到任何问题,请告诉我不幸的是,仍然没有打印或传递到下一个视图。嗯。。。你能编辑你的问题并向我展示你添加我的指令的控制器代码吗?好的,我刚刚编辑了我问题中的代码,请检查,谢谢orz@TimmyMi您在哪里初始化selectedRows,如var selectedRows=[Int]()。。。。我在你的代码中看不到它
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)