Post

Replies

Boosts

Views

Activity

Segue from search results view controller to another view controller
There is a search controller with two scopes of decks and cards. I set up a search for these two models, but now the task is that when I tap on these results, for decks should be one segue for cards another. In the pictures, you can see, after I typed search text, it shows results correctly, but when you tap on the result row, nothing happens. Can't find any solution for almost a week... class DecksTableViewController: UITableViewController { private var searchController: UISearchController! private var decks: Results<Deck>!   override func viewDidLoad() {     super.viewDidLoad() tableView.tableFooterView = UIView() let resultsTableController = self.storyboard?.instantiateViewController(withIdentifier: "ResultsTableViewController") as! ResultsTableViewController configureSearchController(resultsTableController) definesPresentationContext = true navigationItem.hidesSearchBarWhenScrolling = true navigationController?.navigationBar.prefersLargeTitles = true   } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) loadDecks() } private func loadDecks() { decks = StorageManager.realm.objects(Deck.self).sorted(byKeyPath: "dateCreated", ascending: true) tableView.reloadData() } private func configureSearchController(_ searchResultsController: ResultsTableViewController) { searchController = UISearchController(searchResultsController: searchResultsController) searchController.searchResultsUpdater = searchResultsController searchController.obscuresBackgroundDuringPresentation = false searchController.searchBar.placeholder = "Search" searchController.searchBar.scopeButtonTitles = ["Decks", "Cards"] searchController.searchBar.delegate = searchResultsController navigationItem.searchController = searchController } } Here the search results controller: import UIKit import RealmSwift class ResultsTableViewController: UITableViewController { var filteredDecks: Results<Deck>? var filteredCards: Results<Card>? var scope: String?   override func viewDidLoad() {     super.viewDidLoad() tableView.delegate = self tableView.tableFooterView = UIView()   }   // MARK: - Table view data source   override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {     // #warning Incomplete implementation, return the number of rows if scope == "Decks" { return filteredDecks?.count ?? 0 } else { return filteredCards?.count ?? 0 }   }       override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {     let cell = tableView.dequeueReusableCell(withIdentifier: "resultsCell", for: indexPath) var content = cell.defaultContentConfiguration() if scope == "Decks" { let deck = filteredDecks?[indexPath.row] content.text = deck?.name } else { let card = filteredCards?[indexPath.row] content.text = card?.front content.secondaryText = card?.back } cell.contentConfiguration = content     return cell   } override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if scope == "Decks" { guard let cardsVC = self.storyboard?.instantiateViewController(identifier: "cardsVC") as? CardsTableViewController else { return } cardsVC.selectedDeck = filteredDecks?[indexPath.row] self.parent?.navigationController?.pushViewController(cardsVC, animated: true) } else { guard let cardDetailVC = self.storyboard?.instantiateViewController(identifier: "cardDetailVC") as? NewCardViewController else { return } cardDetailVC.selectedCard = filteredCards?[indexPath.row] self.navigationController?.pushViewController(cardDetailVC, animated: true) } tableView.deselectRow(at: indexPath, animated: true) } } extension ResultsTableViewController: UISearchResultsUpdating { func updateSearchResults(for searchController: UISearchController) { let searchBar = searchController.searchBar let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex] filterContentForSearchText(searchController.searchBar.text!, scope: scope) } private func filterContentForSearchText(_ searchText: String, scope: String) { var decks: Results<Deck>? var cards: Results<Card>? if scope == "Cards" { cards = StorageManager.realm.objects(Card.self).sorted(byKeyPath: "front") filteredCards = cards?.filter("front CONTAINS[c] '\(searchText)'") } else { decks = StorageManager.realm.objects(Deck.self).sorted(byKeyPath: "name") filteredDecks = decks?.filter("name CONTAINS[c] '\(searchText)'") } self.scope = scope tableView.reloadData() } } extension ResultsTableViewController: UISearchBarDelegate { func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) { filterContentForSearchText(searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope]) } }
2
0
811
Jul ’21