Я пытаюсь получить что-то вроде страницы исследования instagram, например здесь (изображение слишком велико, чтобы добавить его в пост). У меня есть viewController для исследования с панелью поиска в верхней части и представлением коллекции для контента под панелью поиска. Каждый раз, когда пользователь нажимает на панель поиска, он go переходит в мой класс контроллера представления таблицы поиска, который дает им результаты пользователи, которые соответствуют их запросу и наоборот.
Вот мой обычный контроллер представления поиска (основной, где он отображает содержимое):
import UIKit
class SearchViewController: UIViewController {
@IBOutlet weak var searchBar: UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
let storyboard = UIStoryboard(name: "Main", bundle: nil)
guard let SearchResultsViewController = storyboard.instantiateViewController(withIdentifier: "SearchResultsViewController") as? SearchTableViewController else { return}
let searchController = UISearchController(searchResultsController: SearchResultsViewController)
searchController.searchResultsUpdater = SearchTableViewController // ERROR HERE
view.addSubview(searchController.searchBar)
definesPresentationContext = true
}
}
и вот мой контроллер представления таблицы поиска:
class SearchTableViewController: UIViewController {
let languages = ["Mandarin Chinese", "English", "Hindustani", "Spanish", "Arabic", "Malay", "Russian", "Bengali", "Portuguese", "French", "Hausa", "Punjabi", "German", "Japanese", "Persian", "Swahili", "Telugu", "Javanese", "Wu Chinese", "Korean"]
var searchResults: [String] = []
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
}
}
extension SearchTableViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
guard let searchText = searchController.searchBar.text else { return }
searchResults = languages.filter { $0.contains(searchText) }
tableView.reloadData()
}
}
extension SearchTableViewController: UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searchResults.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell", for: indexPath)
let language = searchResults[indexPath.row]
cell.textLabel?.text = language
return cell
}
}
At в тот момент, когда я застрял при вызове searchResultsUpdating
класса. С ошибкой Cannot assign value of type 'SearchTableViewController.Type' to type 'UISearchResultsUpdating?'
. У моего SearchTableViewController
есть делегат UISearchResultsUpdating
в расширении класса, я не знаю, где я ошибся? Я пытаюсь добиться чего-то вроде ответа Пита с таким дизайном:
![enter image description here](https://i.stack.imgur.com/8FlRj.gif)
Очевидно, без панели вкладок.