使用UISearchController在UITableView单元格上进行Peek和Pop失败 [英] Peek and Pop on UITableView cells fails with UISearchController

查看:67
本文介绍了使用UISearchController在UITableView单元格上进行Peek和Pop失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Peek and Pop正在使用 UISearchController .但是,一旦您开始使用 updateSearchResults 搜索表,Peek and Pop将停止工作.

Peek and Pop is working with a UISearchController. However, Peek and Pop stops working once you start searching the table using updateSearchResults.

我已经扩展了Apple的

I've extended Apple's Table Search with UISearchController demo to support Peek and Pop as an example:

问题是当我开始搜索表格时,Peek and Pop不再起作用.它只是选择突出显示它:

Problem is when I start searching the table, Peek and Pop doesn't work anymore. It just select highlights it:

我对 MainTableViewController 所做的更新是:

class MainTableViewController: BaseTableViewController, UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating {
    override func viewDidLoad() {
        super.viewDidLoad()
        ...
        if traitCollection.forceTouchCapability == .available {
            registerForPreviewing(with: self, sourceView: tableView)
        }
    }
}

extension MainTableViewController: UIViewControllerPreviewingDelegate {

    func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
        guard let indexPath = tableView?.indexPathForRow(at: location),
            let cell = tableView?.cellForRow(at: indexPath),
            let controller = storyboard?.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController
                else { return nil }

        previewingContext.sourceRect = cell.frame

        controller.product = products[0]

        return controller
    }

    func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
        guard let controller = viewControllerToCommit as? DetailViewController else { return }
        controller.product = products[0]
        show(controller, sender: self)
    }
}

搜索上下文控制器是否干扰了peek和pop(甚至可能是键盘)?当表最初包含所有数据时,我可以使它工作,但是一旦我开始使用搜索,它就无法工作.如果您想运行它并看到问题,我在此处附加了工作示例.

Is the search context controller interfering with peek and pop (could even be the keyboard)? I can get it to work when the table initially all data, but it does not once I start using the search. I attached a working sample here if you want to run it and see the issue.

推荐答案

首先,在您的 MainTableViewController.viewDidLoad()中,您还需要注册 resultsTableController.tableView ,因为那是一个单独的视图,将接收窥视/流行信息:

First, in your MainTableViewController.viewDidLoad() you need to also register your resultsTableController.tableView, since that is a separate view that will receive peek/pop information:

if traitCollection.forceTouchCapability == .available {
    previewingContext = registerForPreviewing(with: self, sourceView: tableView)
    if let resultVC = searchController.searchResultsController as? ResultsTableController {
        resultVC.registerForPreviewing(with: self, sourceView: resultVC.tableView)
    }
}

在测试此解决方案时,我注意到一个奇怪的问题,即结果集中的第一行不可窥视,而结果集中的空白行则可窥视.因此, previewingContext(_:viewControllerForLocation:):

When testing this solution, I noticed a strange problem, that the first row in the result set wasn't peekable, and blank rows in the result set WERE peekable. So, the second fix in previewingContext(_:viewControllerForLocation:):

func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
    guard let tableView = previewingContext.sourceView as? UITableView,
        let indexPath = tableView.indexPathForRow(at: location),

在您的原始代码中,它使用的是 MainTableViewController 上的 tableView 属性,而不是 sourceView 进行交互.

In your original code, it was using the tableView property on the MainTableViewController instead of the tableView that was the sourceView for the interaction.

现在,此功能在您搜索和不在搜索时都有效.但是,当您输入搜索但尚未输入任何搜索文本时, UISearchController 是活动的,但是 UITableView MainTableViewController中的一个,并且您无法将视图两次注册为源视图.因此,我们还有更多工作要做:

Now, this works when you're searching, and when you're not. However, when you've entered the search, but haven't entered any search text yet, the UISearchController is active, but the UITableView is the one from MainTableViewController, and you cannot register a view as a source view twice. So, we have a little more work to do:

// local property to store the result from registerForPreviewing(with:sourceView:)
var previewingContext: UIViewControllerPreviewing?

func didPresentSearchController(_ searchController: UISearchController) {
    if let context = previewingContext {
        unregisterForPreviewing(withContext: context)
        previewingContext = searchController.registerForPreviewing(with: self, sourceView: tableView)
    }
}

func didDismissSearchController(_ searchController: UISearchController) {
    if let context = previewingContext {
        searchController.unregisterForPreviewing(withContext: context)
        previewingContext = registerForPreviewing(with: self, sourceView: tableView)
    }
}

基本上,当显示 UISearchController 时,我们取消注册 MainTableViewController 并注册搜索控制器.当它被撤消时,我们进行相反的操作.

Basically, when the UISearchController is presented, we unregister MainTableViewController and register the search controller. When it is dismissed, we do the reverse.

有了这些更改,便可以在所有三种状态下进行窥视和弹出.

With these changes, peek and pop work in all three states.

这篇关于使用UISearchController在UITableView单元格上进行Peek和Pop失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆