Ios 搜索控制器无结果(空白页)
基本表正确显示了结果-它工作正常 我对显示搜索结果的表有问题,总是返回白色页面。(无结果)问题可能是一个控制器中有两个表 我做错了什么Ios 搜索控制器无结果(空白页),ios,swift,uitableview,search,uisearchcontroller,Ios,Swift,Uitableview,Search,Uisearchcontroller,基本表正确显示了结果-它工作正常 我对显示搜索结果的表有问题,总是返回白色页面。(无结果)问题可能是一个控制器中有两个表 我做错了什么 class SearchMagazineViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating { @IBOutlet var tableView: UITableView! var results
class SearchMagazineViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating {
@IBOutlet var tableView: UITableView!
var resultsController: UITableViewController!
var searchController: UISearchController!
var searchMagazines: [Magazine]?
var latestPub = [Magazine]()
func updateSearchResults(for searchController: UISearchController) {
let searchText = searchController.searchBar.text!
if searchText.count > 0 {
self.searchMagazines = CoreDataHandler.searchMagazine(query: searchText, scope: 0)
}
self.resultsController.tableView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
latestPub = CoreDataHandler.latestMagazines() ?? [Magazine]()
resultsController = UITableViewController(style: .plain)
resultsController.tableView.register(UINib(nibName: "searchCell", bundle: nil), forCellReuseIdentifier: "searchCell")
resultsController.tableView.dataSource = self
resultsController.tableView.delegate = self
searchController = UISearchController(searchResultsController: resultsController)
searchController.delegate = self
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = true
navigationItem.searchController = searchController
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.resultsController {
return searchMagazines?.count ?? 0
}else{
return latestPub.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == self.resultsController {
let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell", for: indexPath) as! SearchCell
let item = searchMagazines![indexPath.row]
cell.cat.text = TitleReturn(id: item.cat)
return cell
}else{
guard let cell = tableView.dequeueReusableCell(withIdentifier: "SearchCell") as? SearchTableViewCell else { return UITableViewCell() }
cell.title.text = latestPub[indexPath.row].title
return cell
}
}
}
纠正以下错误。if tableView==self.resultsController{谢谢,我改为“if tableView==resultsController.tableView{”,但现在出现了一个错误“线程1:致命错误:在展开可选值时意外发现了nil”