我知道这是一个常见的问题,但我试图在searchBar委托中调用textDidChange函数。我能够调用updateSearchResults函数,但由于某种原因我无法调用textDidChange(在textDidChange函数中打印时不显示print语句)。作为最终目标,我试图根据搜索文本输入过滤我的collectionView - searchBar中的“A”文本将仅返回其“name”字典值中带有第一个字母“A”的用户。当用户在searchPeople searchBar中输入一些文本时,我可以加载SearchPeopleResultsVC中的所有用户,但在搜索栏中输入一个字母时没有任何反应。
踢球者是我有一个“menuBar”collectionView类,其中每个单元格委托给另一个collectionView。我遇到问题的是searchPeopleController(indexPath 1)。请参阅下面的屏幕截图和代码。提前谢谢!
documentation on linking account providers
//数据模型类
class SearchVC: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UISearchControllerDelegate, UISearchBarDelegate {
var users = [CurrentTraveler]()
var isSearching = false
var filteredData = [CurrentTraveler]()
var peopleResultsViewController: SearchPeopleResultsVC?
var searchPeopleTableView: SearchPeopleCollectionView?
var filtered:[String] = []
var searchActive : Bool = false
var searchPeopleController: UISearchController?
let cellId1 = "cellId1"
let cellId2 = "cellId2"
lazy var contentCollectionView: UICollectionView = {
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
return cv
}()
override func viewDidLoad() {
super.viewDidLoad()
peopleResultsViewController?.collectionView.delegate = self
peopleResultsViewController?.collectionView.dataSource = self
searchPeopleController?.searchBar.delegate = self
}
func scrollToMenuIndex(menuIndex: Int) {
let indexPath = IndexPath(item: menuIndex, section: 0)
contentCollectionView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition(), animated: true)
if indexPath.item == 1 {
searchPeopleController?.delegate = self
searchPeopleController?.searchBar.delegate = self
peopleResultsViewController = SearchPeopleResultsVC()
searchPeopleController = UISearchController(searchResultsController: peopleResultsViewController)
searchPeopleController?.searchResultsUpdater = peopleResultsViewController
searchPeopleController?.searchBar.resignFirstResponder()
navigationItem.titleView = searchPeopleController?.searchBar
definesPresentationContext = true
}
if indexPath.item == 0 {
// sets up place collectionview...
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId1", for: indexPath)
if indexPath.item == 0 {
// return first collectionView for places...
}
if indexPath.item == 1 {
let usersCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId2", for: indexPath) as! SearchPeopleCollectionView
usersCell.searchVC = self
return usersCell
}
return cell
}
}
//包含menuBar collectionView
的searchVC类class SearchPeopleResultsVC : UIViewController, UISearchResultsUpdating, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UISearchBarDelegate {
let cellId = "cellId"
var users = [CurrentTraveler]()
var filteredData = [CurrentTraveler]()
var isSearching = false
var searchVC: SearchVC?
var peopleResultsViewController: SearchPeopleResultsVC?
var filtered:[String] = []
var searchActive : Bool = false
let searchPeopleController = UISearchController(searchResultsController: nil)
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.dataSource = self
cv.delegate = self
return cv
}()
override func viewDidLoad() {
collectionView.delegate = self
collectionView.dataSource = self
searchPeopleController.searchBar.delegate = self
fetchTravelingUserCell()
}
func fetchTravelingUserCell() {
Database.database().reference().child("users").observe(.childAdded, with: { (snapshot) in
if let dictionary = snapshot.value as? [String: AnyObject] {
let user = CurrentTraveler(dictionary: dictionary)
self.users.append(user)
DispatchQueue.main.async(execute: {
self.collectionView.reloadData()
})
}
}, withCancel: nil)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if searchPeopleController.isActive && searchPeopleController.searchBar.text != "" {
return users.count
}
return filteredData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as? BaseGlobalSearchUser else { fatalError("Expected to display a `DataItemCollectionViewCell`.") }
if searchPeopleController.isActive && searchPeopleController.searchBar.text != "" {
cell.currentTraveler = filteredData[indexPath.item]
} else {
cell.currentTraveler = users[indexPath.item]
}
return cell
}
var filterString = "" {
didSet {
guard filterString != oldValue else { return }
if filterString.isEmpty {
filteredData = users
}
else {
filteredData = users.filter { ($0.name?.localizedStandardContains(filterString))! }
}
self.collectionView.reloadData()
}
}
func updateSearchResults(for searchPeopleController: UISearchController) {
filterString = searchPeopleController.searchBar.text ?? ""
filteredData = users
filteredData = users.filter({$0.name?.range(of: filterString) != nil})
self.collectionView.reloadData()
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
searchActive = true
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
searchActive = false
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searchActive = false
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchActive = false
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filterString = searchPeopleController.searchBar.text ?? ""
print(filterString)
}
}
//用于People collectionView搜索栏的searchResultsVC
use_legacy_sql = FALSE