简体   繁体   English

如何获取搜索结果以滚动表格视图的顶部?

[英]How to get search results to scroll the top of the tableview?

Previously my search results were showed clipped and I had to scroll up to down to see the full results以前我的搜索结果显示为剪裁,我必须上下滚动才能看到完整的结果

Where I want the Results to jump to the top of the tableview as soon as I start typing something in my search bar.一旦我开始在搜索栏中输入内容,我希望结果跳到表格视图的顶部。

the code im using to make it work, works the first time but then I get the SIGABRT error in the AppDelegate as soon as I type in a new search.我用来使其工作的代码第一次工作,但是一旦我输入新的搜索,我就会在 AppDelegate 中收到SIGABRT错误。

productListTableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)

▲ there's the code that used to make the search results show at the top of the tableview but as soon as I type something new the simulator closes out on me. ▲ 有用于使搜索结果显示在 tableview 顶部的代码,但只要我输入新内容,模拟器就会关闭我。 im trying to get hate results presented to the top everytime I punch in a new search每次我进行新搜索时,我都试图将讨厌的结果显示在顶部

extension ProductListController : UISearchBarDelegate, UISearchDisplayDelegate {

        func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
            productInventory = self.productSetup.filter({ (products) -> Bool in
                return products.name.range(of: searchText, options: [ .caseInsensitive, .diacriticInsensitive ]) != nil 
                })

        self.productListTableView.reloadData()

      // Code that scrolls search results to the top of the tableview ▼

        self.productListTableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
    }


    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        searchActive = true;
    }

    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        searchActive = false;
        self.searchBar.endEditing(true)
    }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        searchActive = false;
        self.searchBar.endEditing(true)
    }

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        searchActive = false;
        self.searchBar.endEditing(true)
    }
}

I've tried using the following from stack to help already but no luck我已经尝试使用堆栈中的以下内容来提供帮助,但没有运气

UITableView - scroll to the top UITableView - 滚动到顶部

How to get a UITableview to go to the top of page on reload? 如何在重新加载时将 UITableview 到 go 放到页面顶部?

How to refresh my UITableView with UISearchBar after click of cancel button of UISearchBar? 单击 UISearchBar 的取消按钮后,如何使用 UISearchBar 刷新我的 UITableView?

First, before scrolling to row (0,0) you need to check that it exists:首先,在滚动到行 (0,0) 之前,您需要检查它是否存在:

if !self.productInventory.isEmpty {
    self.productListTableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
}

second, reloadData() may not happen immediately - it may happen on the next run loop.其次, reloadData()可能不会立即发生 - 它可能会发生在下一个运行循环中。 So you should wrap the scroll in a dispatch block:所以你应该将滚动包装在一个调度块中:

DispathQueue.main.async {
    if !self.productInventory.isEmpty {
        self.productListTableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM