我知道这在某种程度上是一个常见问题,但我试图在我的 searchBar 委托中调用 textDidChange 函数。我可以调用 updateSearchResults 函数,但由于某种原因,我无法调用 textDidChange(在 textDidChange 函数中打印时不显示打印语句)。作为最终目标,我试图根据搜索文本输入过滤我的 collectionView - searchBar 中的“A”文本将仅返回“名称”字典值中第一个字母“A”的用户。当用户在 searchPeople 搜索栏中输入一些文本时,我能够在 SearchPeopleResultsVC 中加载所有用户,但是在搜索栏中输入字母时没有任何反应。
最重要的是我有一个“menuBar”collectionView 类,每个单元格都委托给另一个collectionView。我遇到的问题是 searchPeopleController (indexPath 1)。请参阅下面的屏幕截图和代码。提前致谢!
// 数据模型类
class CurrentTraveler: SafeJsonObject {
var name: String?
var profileImageUrl: String?
var travelingPlace: String?
init(dictionary: [String: AnyObject]) {
super.init()
self.name = dictionary["name"] as? String
self.profileImageUrl = dictionary["profileImageUrl"] as? String
self.travelingPlace = dictionary["users/uid/Places"] as? String
setValuesForKeys(dictionary)
}
}
// 保存 menuBar 集合视图的 searchVC 类
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
}
}
// 用于 People collectionView 搜索栏的 searchResultsVC
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)
}
}