好的,我希望这是有道理的:
所以我想要做的就是将用户名过滤到我从firebase获得的collectionView中。我完成了所有设置,它获取用户名并在单击搜索栏时将其显示在集合视图中。当我开始在搜索栏上输入时,它会过滤用户名但不在UI上。
原因是,因为点击过滤用户名时显示的搜索栏和collectionViews 位于不同的类中。
还不知道我想说什么?这就是我的意思:
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
}
已经注释掉的代码会很好但是它不会让我编译错误'实例成员' searchBar不能用于类型' UserSearchController&#39 ;。
var users =用户示例数据:
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 ?? ""
}
}
如果您对解决问题有任何疑问,请与我们联系。谢谢!
答案 0 :(得分:1)
与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()
}