-1

I am trying to create a joke app. The search bar works. But when I select the search results, it won't take me to the tableview( the jokes). How can I fix it? Thanks everyone in advance.

enter image description here enter image description here

import UIKit

class JokeTableViewController: UITableViewController, UISearchResultsUpdating {


    var jokes = [ "chiken", "Walk into A Bar", "Olives", "Racer", "love"]
    var filteredJokes = [String]()

    var searchController : UISearchController!
    var resultsController = UITableViewController()

    override func viewDidLoad() {
        super.viewDidLoad()

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

        self.searchController = UISearchController(searchResultsController: self.resultsController)
        self.tableView.tableHeaderView = self.searchController.searchBar
        self.searchController.searchResultsUpdater = self
        //self.searchController.dimsBackgroundDuringPresentation = false

    }

    func updateSearchResults(for searchController: UISearchController) {
        self.filteredJokes = self.jokes.filter { (jokee:String) -> Bool in
            if jokee.lowercased().contains(self.searchController.searchBar.text!.lowercased()){
                return true
            }else {
                return false
            }
        }
        //Update the results TableView
        self.resultsController.tableView.reloadData()

    }


    //WHEN SELECTED TO TO THE JOKES


    // HOW MANY?
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == self.tableView{
            return self.jokes.count
        } else  {
            return self.filteredJokes.count
        }
    }

    //WHAT GOES INSIDE?

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()

        if tableView == self.tableView{
            cell.textLabel?.text = self.jokes[indexPath.row]
        } else{
            cell.textLabel?.text = self.filteredJokes[indexPath.row]
        }

        return cell
    }

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


        let selectedJoke = jokes[indexPath.row]

        performSegue(withIdentifier: "moveToJokeDefinition", sender: selectedJoke)


    }

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

            if let selectedJoke = sender as? String {
             jokeVC.joke = selectedJoke
            }
           //select the jokes

        }
    }

}
vadian
  • 274,689
  • 30
  • 353
  • 361
benjamin
  • 1
  • 2
  • Hi there, I think I've seen similar question around here. Maybe [this one](https://stackoverflow.com/questions/28438373/how-do-you-dismiss-a-uisearchcontroller-ios-8-and-follow) can help you. Also try to reduce your code sample to a minimum. For example, we do't need the prepareForSegue or numberOfRows methods. That's kinda distracting and makes it harder to help. – jraufeisen Mar 19 '20 at 18:53
  • 1
    Does this answer your question? [How do I dismiss a UISearchController view?](https://stackoverflow.com/questions/38726205/how-do-i-dismiss-a-uisearchcontroller-view) – jraufeisen Mar 19 '20 at 18:55
  • 1
    Thanks @JoRa, this is my first time asking a question here so Im not too familiar with the guidelines. Thanks for your the input. – benjamin Mar 19 '20 at 19:06

1 Answers1

0

You should use one global variable. var globaldata = String self.globaldata = self.jokes .... initData............

you should use this data in tableView Delegate functions.

Chan Lung
  • 15
  • 1
  • 5
  • Hi Chan. Try using a markdown code fence (3 backticks in a row ) to format the code in your answer{ ``` line by line code in here ``` – Love and peace - Joe Codeswell Mar 19 '20 at 19:46
  • Yes. He is using same data in this code. In didselect function,,, he is using original data ,. { var jokes = [ "chiken", "Walk into A Bar", "Olives", "Racer", "love"] } If search bar works, then tableview datasource should be updated. In this code, he was not using updated data . line here......... { override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ let selectedJoke = jokes[indexPath.row] performSegue(withIdentifier: "moveToJokeDefinition", sender: selectedJoke) } } – Chan Lung Mar 19 '20 at 20:00
  • thanks for the answer. I think i got the idea. Can you complete the code @ChanLung? – benjamin Mar 20 '20 at 15:14