3d 偷看 &弹出搜索结果 [英] 3d Peek & Pop for search results
问题描述
我使用搜索栏来过滤我的表格视图.我在显示可搜索内容的同一个视图控制器中显示搜索结果.它工作正常.除了一件事 - 3d Peek and Pop 不适用于搜索结果.我确实尝试过Peek &"在 Storyboard segue 中弹出复选框.我确实尝试过编程 peek &弹出我的 TableViewController.结果是一样的:peek &pop 适用于表格视图单元格,但不适用于搜索结果单元格.
I use a Search Bar to filter my Table View. I display the search results in the same view controller that displays the searchable content. It works fine. Except one thing - 3d Peek and Pop is not working for the search results. I did tried the ‘Peek & Pop’ check box in Storyboard segue. I did tried programming peek & pop in my TableViewController. Result is the same: peek & pop works for table view cells but not for search result cells.
这里是搜索和查看 &弹出代码片段:
Here is the search and peek & pop code pieces:
// 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 &弹出搜索结果?
Has anyone any ideas how to implement peek & pop for search results?
我想主要问题是如何注册预览 UISearchController?..
I guess the main problem is how to register for previewing the UISearchController?..
推荐答案
我成功了.有几个新来的孩子"错误.正确的代码:
I made it work. There were couple of 'new kid on the block' mistakes. The correct code:
// 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
In short: 1) added delegating for searchController 2) using searchResults in UIViewControllerPreviewingDelegate if searchController.isActive
希望对某人有所帮助.
这篇关于3d 偷看 &弹出搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!