好吧,我希望这将是有意义:搜索栏有困难过滤SWIFT 3
那么我现在要做的是过滤器的用户名,到我从火力得到的CollectionView。我已经完成了所有设置,当我单击搜索栏时,它会提取用户名并将其显示到集合视图中。当我开始在搜索栏上输入内容时,它会过滤用户名,但不在用户界面上。
这样做的原因是因为搜索栏和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
}
的代码已经被评论出去会很好b它不会让我编译一个错误'实例成员'searchBar不能用于'UserSearchController'类型。
VAR用户=用户样本数据:
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 ?? ""
}
}
让我知道,如果你有了解该问题的任何问题。谢谢!
请解释“它会过滤用户名,但不在用户界面上”和图像哟你说“打字时不想过滤”? – 3stud1ant3
@ 3stud1ant3当然。我试图说的是,它打印输入时发生在“textDidChange”方法中发生的过滤。但收集视图根本不会过滤。它保持不变,就像没有任何事情发生一样 –
也请解释你展示的两幅图像之间的差异,所以我可以理解你的应用程序的流程还是它们是相同的? – 3stud1ant3