好的,我希望这是有道理的:
所以我要做的是将用户名过滤到我从 firebase 获得的 collectionView 中。我已经全部设置好,当我单击搜索栏时,它会获取用户名并将其显示到 Collection View 中。当我开始在搜索栏上打字时,它会过滤用户名,但不会在 UI 上过滤。
The reason for this is because the search bar and the collectionViews it shows when clicked to filter usernames, are in different classes.
还是不知道我想说什么?这就是我的意思:
click here for the searchBar & CollectionViewController together picture
click here to for the searchBar & collection View
UserSearchController 相关代码:
var users = [User]()
func fetchUsers() {
let ref = FIRDatabase.database().reference().child("users")
ref.observe(.value, with: { (snapshot) in
guard let dictionaries = snapshot.value as? [String: Any] else { return }
//For each iterates through every object in the dictioary
dictionaries.forEach({ (key, value) in
guard let userDictionary = value as? [String: Any] else { return}
let user = User(uid: key, dictionary: userDictionary)
self.users.append(user)
print(user.uid, user.username)
})
self.collectionView?.reloadData()
}) { (error) in
print("failed to fetch users:", error)
}
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
self.users = self.users.filter { (user) -> Bool in
print(",here:",user)
return user.username.contains(searchText)
}
self.collectionView?.reloadData()
}
带有相关代码的UserSearchCV:
var users = [User]()
func fetchUsers() {
let ref = FIRDatabase.database().reference().child("users")
ref.observe(.value, with: { (snapshot) in
guard let dictionaries = snapshot.value as? [String: Any] else { return }
//For each iterates through every object in the dictioary
dictionaries.forEach({ (key, value) in
guard let userDictionary = value as? [String: Any] else { return}
let user = User(uid: key, dictionary: userDictionary)
self.users.append(user)
print(user.uid, user.username)
})
self.collectionView?.reloadData()
}) { (error) in
print("failed to fetch users:", error)
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//let numberOfUsers = UserSearchController.searchBar(users.count) did you mean to use a value of this type instead?
return users.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! UserSearchCVCell
// let filteredUsers = UserSearchController.searchBar(users.count)
// cell.user = filteredUsers[indexPath.item]
cell.user = users[indexPath.item]
return cell
}
被注释掉的代码会很好,但它不会让我编译错误“Instance member”searchBar cannot be used on type“UserSearchController”。
var users = User 示例数据:
struct User {
let uid: String
let username: String
let profileImageUrl: String
let videos: String
init(uid: String, dictionary: [String: Any]) {
self.uid = uid
self.username = dictionary["username"] as? String ?? ""
self.profileImageUrl = dictionary["profileImageUrl"] as? String ?? ""
self.videos = dictionary["Videos"] as? String ?? ""
}
如果您对理解问题有任何疑问,请告诉我。谢谢!
最佳答案
经过与OP的讨论,我们找到了解决方案
请遵循这些
首先更新你的 searchBar 方法如下
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
self.users = self.users.filter { (user) -> Bool in
print(",here:",user)
delegate?.passFilterArray(self.users)
return user.username.contains(searchText)
}
searchUsersCV.isHidden = true
searchUsersCV.updateUsersView(self.users)
self.collectionView?.reloadData()
}
然后在 UserSearchCV 中添加这个
func updateUsersView(_ users: [User]) {
self.users = users
print(self.users)
self.collectionView.reloadData()
}
关于ios - searchBar 无法过滤 swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45932307/