菜鸟教程小白 发表于 2022-12-11 20:13:44

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


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

<pre><code>class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating{

@IBOutlet weak var mainTableView: UITableView!

var words = ()
var filteredArray = ()
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) -&gt; Bool in
      if words.word.contains(searchController.searchBar.text!){
            return true
      }else{
            return false
      }
    })
    resultsController.tableView.reloadData()
}

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

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

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: &#34;wordDetails&#34;, 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 () -&gt; ()){
    let path = Bundle.main.path(forResource: &#34;week_14&#34;, ofType: &#34;json&#34;)
    let url = URL(fileURLWithPath: path!)
    let data = try! Data(contentsOf: url)
            do {
                self.words = try JSONDecoder().decode(.self, from: data)
                DispatchQueue.main.async{
                  completed()
                }
            }catch{
                print(&#34;JSON error&#34;)
            }
}
</code></pre>

<p>}</p>

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

<pre><code>struct wordStats:Decodable{
let word: String
let synonym: String
let meaning: String
let example: String
let video: String
</code></pre>

<p>}</p>

<p>这是单词统计</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>发布崩溃日志。根据我们在这里所拥有的信息,我的猜测是您在 <code>prepareForSegue</code> 中崩溃了:</p>

<pre><code>destination.word = words[(mainTableView.indexPathForSelectedRow?.row)!]
</code></pre>

<p>使用过滤结果时可能是索引错误。这可能会解决它:</p>

<pre><code>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)!]
            }
      }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何从表格 View 中显示的搜索结果导航到另一个 View ?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/50707783/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/50707783/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何从表格 View 中显示的搜索结果导航到另一个 View ?