gpt4 book ai didi

ios - 如何从 TableView 中显示的搜索结果导航到另一个 View ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:02:42 24 4
gpt4 key购买 nike

以下代码是我的 viewController 代码。来自 json 文件的单词显示在 tableView 中,点击一个单词将其带到显示与该单词关联的元素的另一个页面。但是,当搜索结果显示在表格 View 中时,我无法导航到该页面。

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating{

@IBOutlet weak var mainTableView: UITableView!

var words = [wordStats]()
var filteredArray = [wordStats]()
var searchController = UISearchController()
var resultsController = UITableViewController()


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
getJson {
self.mainTableView.reloadData()
}

mainTableView.delegate = self
mainTableView.dataSource = self

searchController = UISearchController(searchResultsController: resultsController)
mainTableView.tableHeaderView = searchController.searchBar
searchController.searchResultsUpdater = self

resultsController.tableView.delegate = self
resultsController.tableView.dataSource = self

}

func updateSearchResults(for searchController: UISearchController) {
filteredArray = words.filter({ (words: wordStats) -> Bool in
if words.word.contains(searchController.searchBar.text!){
return true
}else{
return false
}
})
resultsController.tableView.reloadData()
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == resultsController.tableView{
return filteredArray.count
}else{
return words.count
}
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
if tableView == resultsController.tableView{
cell.textLabel?.text = filteredArray[indexPath.row].word.capitalized
}else{
cell.textLabel?.text = words[indexPath.row].word.capitalized
}
//cell.textLabel?.text = words[indexPath.row].word.capitalized
return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "wordDetails", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? HeroViewController{
destination.word = words[(mainTableView.indexPathForSelectedRow?.row)!]
}
}

func getJson(completed: @escaping () -> ()){
let path = Bundle.main.path(forResource: "week_14", ofType: "json")
let url = URL(fileURLWithPath: path!)
let data = try! Data(contentsOf: url)
do {
self.words = try JSONDecoder().decode([wordStats].self, from: data)
DispatchQueue.main.async{
completed()
}
}catch{
print("JSON error")
}
}

最初从 JSON 文件中获取数据后,数据显示在 mainTableView 中,但在搜索后当我单击该项目时应用程序崩溃了。有人可以帮帮我吗?

struct wordStats:Decodable{
let word: String
let synonym: String
let meaning: String
let example: String
let video: String

这是wordStats

最佳答案

发布崩溃日志。根据我们这里所掌握的信息,我的猜测是您在 prepareForSegue 中崩溃了:

destination.word = words[(mainTableView.indexPathForSelectedRow?.row)!]

可能是使用过滤后的结果时出现索引错误。这可能会解决它:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? HeroViewController{

if tableView == resultsController.tableView {
destination.word = filteredArray[(mainTableView.indexPathForSelectedRow?.row)!]
} else {
destination.word = words[(mainTableView.indexPathForSelectedRow?.row)!]
}
}
}

关于ios - 如何从 TableView 中显示的搜索结果导航到另一个 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50707783/

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