updateSearchResults()未被调用

时间:2017-01-10 03:46:01

标签: search uisearchcontroller searchbar

我已经在SO上阅读过类似的问题和解决方案。但似乎没有一个能解决我的问题。我正在使用自定义搜索控制器和自定义搜索栏, func updateSearchResults(对于searchController:UISearchController)未被调用。

    var customSearchController: CustomSearchViewController!

CustomSearchViewController:在ViewDidLoad()

customSearchController = CustomSearchViewController(searchResultsController: ***nil***, searchBarFrame: CGRect(x: 0.0, y: 0.0, width: searchTableView.frame.size.width, height: 44.0), searchBarFont: UIFont(name: "HelveticaNeue", size: 16.0)!, searchBarTextColor: UIColor.purple, searchBarTintColor: UIColor.white)

customSearchController.searchResultsUpdater  = self
customSearchController.definesPresentationContext = true
customSearchController.customSearchBar.placeholder = "What are you looking for?"
customSearchController.customSearchBar.backgroundColor = UIColor.white
customSearchController.customSearchBar.sizeToFit()
customSearchController.customSearchBar.resignFirstResponder()
customSearchController.customSearchBar.showsCancelButton = true
customSearchController.customSearchBar.delegate = self

没有被召唤::(

func updateSearchResults(for searchController: UISearchController) {
    filtered.removeAll()
    filtered = searchArray.filter({ (text) -> Bool in
        let tmp: NSString = text as NSString
        let range = tmp.range(of: customSearchController.customSearchBar.text!, options: NSString.CompareOptions.caseInsensitive)
        return range.location != NSNotFound
    })
    self.searchTableView.reloadData()
}

4 个答案:

答案 0 :(得分:8)

经过几个小时的努力,我能够通过以下方式解决它: func searchBar(_ searchBar:UISearchBar,textDidChange searchText:String) - UISearchBarDelegate委托方法。

而不是updateSearchResults() - UISearchResultsUpdating委托方法

希望它可以帮助某人:)

答案 1 :(得分:2)

您似乎没有设置此设置:

searchController.searchResultsUpdater = self

确保这是最后一条命令,以避免出现错误。至少这对我有用。

答案 2 :(得分:2)

我必须在类范围内声明UISearchController实例。看到这个答案。 https://stackoverflow.com/a/46781890/1511978

以前我是在viewDidLoad方法中声明的

答案 3 :(得分:0)

但是也许您的问题是您打电话给了

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import App from './App';
import './index.css';
import registerServiceWorker from './registerServiceWorker';

import { Provider } from 'react-redux';

import { createStore, compose, applyMiddleware } from 'redux';
import rootReducer from './store/reducers';

import thunk from 'redux-thunk';

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

 const store = createStore(rootReducer, composeEnhancers(
     applyMiddleware(thunk)
 ));

ReactDOM.render(  <Provider store={store}><App /></Provider>, document.getElementById('root'));

registerServiceWorker();

相反,您应该这样做:

navigationItem.titleView = searchController.searchBar