【发布时间】:2017-08-29 20:02:10
【问题描述】:
好的,我希望这是有道理的:
所以我想要做的是将用户名过滤到我从 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”。
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 ?? ""
}
}
如果您对理解问题有任何疑问,请告诉我。谢谢!
【问题讨论】:
-
请解释“它会过滤用户名,但不在 UI 上”,并且在图片中您说“键入时不想过滤”?
-
@3stud1ant3 当然。我想说的是它在键入时打印了“textDidChange”方法中发生的过滤。但是集合视图根本不过滤。它保持不变,就像什么都没发生一样
-
另外请说明您展示的两张图片之间的区别,以便我了解您的应用程序的流程还是它们相同?
-
好的。所以在第一张图片中,searchBar 和集合视图位于名为“UserSearchController”的类中。在第二张图片中,这就是您单击 searchBar 时显示的内容。现在,这样做的目的是为了表明向用户展示的 searchBar 和 collectionView 是两个不同的类。
-
@3stud1ant3 显示用户的 collectionView 位于“UserSearchCV”类中。不在“UserSearchController”类中。因此,如果您在上面我提供的代码中看到,一些代码被注释掉了,因为这是我想做的,但这是不可能的。
标签: ios swift firebase firebase-realtime-database