基本表可以正确显示结果-可以正常工作。
我对显示搜索结果的表有问题,总是返回白页。 (无结果)问题可能出在一个控制器中有两个表吗?
我在做什么错了?
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
}
}
}