2017-03-12 68 views
0

我使用搜索欄來過濾我的表格視圖。我在顯示可搜索內容的相同視圖控制器中顯示搜索結果。它工作正常。除了一件事 - 3D Peek and Pop不適用於搜索結果。 我曾嘗試Storyboard segue中的'Peek & Pop'複選框。我曾嘗試編寫我的TableViewController中的pop pop &。結果是一樣的:peek & pop適用於表格視圖單元格,但不適用於搜索結果單元格。3D Peek&Pop搜索結果

這裏是搜索和PEEK &彈出代碼片段:

// 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) 
    } 

有任何人任何想法如何實現偷看&流行的搜索結果?

我想主要的問題是如何爲預覽UISearchController?..

+0

[UITableView單元上的Peek和Pop失敗,並且UISearchController失敗](http://stackoverflow.com/questions/42196749/peek-and-pop-on-uitableview-cells-fails-with-uisearchcontroller) –

+1

@ DaveWeston Thanx爲您的輸入!我確實檢查了你的建議,但不幸的是它不適用於我。也許是因爲我爲搜索結果重複使用了相同的表視圖控制器。在你提到的主題中,一個單獨的視圖控制器用於搜索結果。 –

回答

0

我做了它的工作寄存器。有幾個'新的孩子'的錯誤。正確的代碼:

// 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)使用SearchResult所中UIViewControllerPreviewingDelegate如果searchController.isActive

希望它可以幫助別人添加爲委託searchController 2)。