gpt4 book ai didi

swift - 如何使用 indexpathforselectedrow 从结构数组中获取实例?

转载 作者:行者123 更新时间:2023-11-30 10:39:30 25 4
gpt4 key购买 nike

我正在为学校构建一个应用程序。该应用程序有点像字典应用程序,其中包括嵌入导航 Controller 中的词汇表(我的 WordsTableViewController),以及显示词汇及其相应定义的定义 View Controller 。

问题是我不知道如何使用indexpathforselectedrow从我的vocabController的vocabWords数组中获取适当的词汇单词。 “词汇表”是位于其自己单独的 swift 文件中的结构实例。

所有这些都将在我的 WordsTableViewController swift 文件中为 segue 做准备。

非常感谢任何帮助。

表格 View 的行数如下所示:

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

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ShowDefinitionSegue" {
if let definitionVC = segue.destination as? DefinitionViewController {
let index = tableView.indexPathForSelectedRow
let indexPathForSelectedRow = index.row
definitionVC.vocabWord[index]
let vocab = myListVocab.vocabWords[indexPathForSelectedRow]
}
}
}

我收到的错误是我对成员 tableView(_:numberOfRowsInSection:) 的引用不明确

到目前为止,我的程序中的其余错误是“只能在类成员上指定覆盖”

最佳答案

首先实现didSelectRow在您的 WordsTableViewController 中。然后创建实例变量selectedWord。之后,您可以通过调用以下命令来获取 didSelectRow 中选定的单词:

selectedWord = myListVocab.vocabWords[indexPath.row]

分配后,您可以在prepareForSegue中使用selectedWord

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ShowDefinitionSegue" {
if let definitionVC = segue.destination as? DefinitionViewController {
destinationVC.word = selectedWord
}
}
}

关于swift - 如何使用 indexpathforselectedrow 从结构数组中获取实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57179215/

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