0

I created a search controller and added a searchResultsController to it.

MainController.swift

let searchController = UISearchController(searchResultsController: SearchResultVC())

My search logic works within the updateSearchResults method and this is triggered every time a search is made. This, of course, is provided by the UISearchResultsUpdating protocol. Here I will add the list I am looking for to the data of the tableView in SearchResultVC and reload the tableView.

MainController.swift

func updateSearchResults(for searchController: UISearchController) {

    guard let searchText = searchController.searchBar.text else { return }

    if let vc = searchController.searchResultsController as? SearchResultVC {
       vc.searchText = searchText
       vc.reminderList = search(searchText: searchText)
       if let vcTableView = vc.searchTableView { // HERE ISN'T TRIGGERED
          vcTableView.dataSource = vc
          vcTableView.delegate = vc
          vcTableView.reloadData()
          print("reloaded")
       }
    }
}

Finally, I will show my data in the newly opened searchViewController.

SearchResultVC.swift

final class SearchResultVC: UIViewController {

    var reminderList: [ReminderList] = []
    var searchText: String?

    @IBOutlet weak var searchTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.searchTableView.delegate = self // HERE IS CRASHED
        self.searchTableView.dataSource = self // HERE IS CRASHED
    }
}

My problem is not being able to reload. The partition where reload () is located is not triggered. Also, the newly opened page is self.searchTableView.delegate = self and self.searchTableView.dataSource = self crashed.

How can I trigger vcTableView.reloadData() correctly ?

EDIT

This error occurs. enter image description here

Alperen ARICI
  • 204
  • 6
  • 13
  • bind `UITableViewDelegate` & `UITableViewDatasource` to your viewcontroller(SearchResultVC) class. –  May 17 '21 at 11:46
  • I didn't put it here but I did. I also realized that the `print("reloaded")` in if is not triggered because it is not. @RB's – Alperen ARICI May 17 '21 at 11:49
  • yes it will not called because there you used `closure` for controller so your controller also need to have completion for call that method. –  May 17 '21 at 11:51
  • How can I add call completion on there ? @RB's – Alperen ARICI May 17 '21 at 11:55
  • I guess your controller was initialized incorrect, check this out: https://stackoverflow.com/questions/67503520/unexpectedly-found-nil-while-implicitly-unwrapping-an-optional-value-uicollecti/67505115#67505115 – son May 17 '21 at 11:55
  • check out here https://stackoverflow.com/a/44540909/9137841 and if you have any difficulty than let me know. –  May 17 '21 at 11:56
  • Thank you for all. I will try and inform. – Alperen ARICI May 17 '21 at 11:59
  • Actually problem is the moment I click on the Search textfield, updateSearchResults is triggered, but SearchResultVC is not created yet as UI. It occurs when I start writing. But it is trying to reload the TableView that was not available at the time I click this search. @RB's – Alperen ARICI May 17 '21 at 12:17
  • please see here https://stackoverflow.com/a/29664483/9137841 –  May 17 '21 at 12:37
  • Thank you. I solved the problem. I was using the storyboard and I had forgot the controller incorrectly. I checked again @son s answer and I solved. We searched for the error in different places. Thank you for everything. :) – Alperen ARICI May 17 '21 at 12:42

0 Answers0