3d Peek & Pop 搜索结果
Posted
技术标签:
【中文标题】3d Peek & Pop 搜索结果【英文标题】:3d Peek & Pop for search results 【发布时间】:2017-03-12 22:07:35 【问题描述】:我使用搜索栏来过滤我的表格视图。我在显示可搜索内容的同一视图控制器中显示搜索结果。它工作正常。除了一件事 - 3d Peek and Pop 不适用于搜索结果。 我确实尝试了 Storyboard segue 中的“Peek & Pop”复选框。我确实尝试在我的 TableViewController 中编程 peek & pop。结果是一样的:peek & pop 适用于表格视图单元格,但不适用于搜索结果单元格。
这里是搜索和 peek & 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 & pop 吗?
我猜主要问题是如何注册预览 UISearchController?..
【问题讨论】:
Peek and Pop on UITableView cells fails with UISearchController的可能重复 @DaveWeston 感谢您的意见!我确实检查了你的建议,不幸的是它对我不起作用。可能是因为我在搜索结果中重用了相同的 Table View Controller。在您提到的主题中,搜索结果使用了一个单独的视图控制器。 【参考方案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
希望对某人有所帮助。
【讨论】:
以上是关于3d Peek & Pop 搜索结果的主要内容,如果未能解决你的问题,请参考以下文章
通过 Longpress 使用 3D touch peek and pop
检测何时取消 3D Touch Peek 和 Pop View
UITableViewCell 中的 3D Touch Peek 和 Pop 如何将数据移交给其他 UIViewController?