我使用搜索栏来过滤我的表格视图。我在显示可搜索内容的同一视图控制器中显示搜索结果。它工作正常。除了一件事 - 3d Peek和Pop不适用于搜索结果。 我确实试过'Peek&在Storyboard segue中弹出'复选框。我确实试过编程peek&在我的TableViewController中弹出。结果是一样的:偷看& pop适用于表格视图单元格,但不适用于搜索结果单元格。
这是搜索和窥视& pop代码片段:
// Add a search bar
searchController = UISearchController(searchResultsController: nil)
tableView.tableHeaderView = searchController.searchBar
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
self.tableView.contentOffset = CGPoint(x: 0, y: searchController.searchBar.frame.height)
// Register for preview
if traitCollection.forceTouchCapability == .available {
previewingContext = registerForPreviewing(with: self, sourceView: tableView)
}
func updateSearchResults(for searchController: UISearchController) {
if let searchText = searchController.searchBar.text {
filterContent(for: searchText)
tableView.reloadData()
}
}
// MARK: UIViewControllerPreviewingDelegate methods
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
guard let tableView = previewingContext.sourceView as? UITableView,
let indexPath = tableView.indexPathForRow(at: location),
let cell = tableView.cellForRow(at: indexPath),
let detailVC = storyboard?.instantiateViewController(withIdentifier: "AlarmViewController") as? AlarmViewController
else { return nil }
detailVC.alarm = alarms[indexPath.row]
previewingContext.sourceRect = cell.frame
return detailVC
}
func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
show(viewControllerToCommit, sender: self)
}
有没有人有任何想法如何实施peek& amp;流行搜索结果?
我想主要的问题是如何注册预览UISearchController?..
答案 0 :(得分:1)
我做到了。有几个“新的孩子在街区”的错误。正确的代码:
// Add a search bar
searchController = UISearchController(searchResultsController: nil)
searchController.loadViewIfNeeded()
tableView.tableHeaderView = searchController.searchBar
searchController.searchResultsUpdater = self
searchController.delegate = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
self.tableView.contentOffset = CGPoint(x: 0, y: searchController.searchBar.frame.height)
// Hide search bar on detailed view and preserve search results
self.definesPresentationContext = true
// Register for preview
if traitCollection.forceTouchCapability == .available {
previewingContext = registerForPreviewing(with: self, sourceView: tableView)
}
func updateSearchResults(for searchController: UISearchController) {
if let searchText = searchController.searchBar.text {
filterContent(for: searchText)
tableView.reloadData()
}
}
// MARK: UIViewControllerPreviewingDelegate methods
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
guard let tableView = previewingContext.sourceView as? UITableView,
let indexPath = tableView.indexPathForRow(at: location),
let cell = tableView.cellForRow(at: indexPath),
let detailVC = storyboard?.instantiateViewController(withIdentifier: "AlarmViewController") as? AlarmViewController
else { return nil }
detailVC.alarm = (searchController.isActive) ? searchResults[(indexPath.row)] : alarms[indexPath.row]
previewingContext.sourceRect = cell.frame
return detailVC
}
func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
show(viewControllerToCommit, sender: self)
}
简而言之: 1)为searchController添加了委托 2)如果searchController.isActive
在UIViewControllerPreviewingDelegate中使用searchResults希望它有所帮助。