Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UISearchResultsUpdate不符合视图控制器中的扩展类_Ios_Swift_Delegates_Uisearchcontroller - Fatal编程技术网

Ios UISearchResultsUpdate不符合视图控制器中的扩展类

Ios UISearchResultsUpdate不符合视图控制器中的扩展类,ios,swift,delegates,uisearchcontroller,Ios,Swift,Delegates,Uisearchcontroller,我正在尝试获取类似instagrams explore页面的内容,示例图像太大,无法添加到帖子中。我有一个explore viewController,在顶部有一个搜索栏,在搜索栏下面有一个内容的集合视图,每当用户单击搜索栏时,它都会转到我的搜索表视图控制器类,该类为他们提供与查询匹配的用户结果,反之亦然 这是我的普通搜索视图控制器,主要用于显示内容: import UIKit class SearchViewController: UIViewController { @IBOutlet

我正在尝试获取类似instagrams explore页面的内容,示例图像太大,无法添加到帖子中。我有一个explore viewController,在顶部有一个搜索栏,在搜索栏下面有一个内容的集合视图,每当用户单击搜索栏时,它都会转到我的搜索表视图控制器类,该类为他们提供与查询匹配的用户结果,反之亦然

这是我的普通搜索视图控制器,主要用于显示内容:

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
}
}
目前,我一直在给SearchResultsUpdate类打电话。错误为,无法将“SearchTableViewController.type”类型的值分配给“UISearchResultsUpdated”类型。我的SearchTableViewController在类的扩展中有UISearchResultSupdated委托,我不知道哪里出了问题?我正试图通过这种设计实现以下目标:


显然没有选项卡导航栏。

您不需要SearchTableViewController的实例吗

您正在尝试在此处指定类类型而不是其实例:

searchController.searchResultsUpdater = SearchTableViewController

您需要初始化SearchTableViewcontroller或将其实例的引用设置为SearchResultsUpdate

yeh…这是一个很好的观点。这让我意识到我引用了错误的实例,我需要引用guard let SearchResultsViewController=storyboard.InstanceEviewController标识符:SearchResultsViewController as?SearchTableViewController else{return}变量,而不是实际的故事板本身,哈哈。从技术上讲,我会把它标记为答案。谢谢是的,有时候很容易忽视并深入寻找解决方案:DJ需要一双新的眼睛来认识愚蠢的错误