2017-08-29 29 views
0

迅速なフィルタリングを持って、私はこれが意味をなすために起こっている願っています:検索バーがトラブルオーケー3

それでは、私がやろうとしているが、私は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 
} 

コメントされたコードは、アウトは素敵ですbそれは 'Instance member'でエラーをコンパイルすることはできません。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 ?? "" 
} 

}

あなたは問題を理解した上でのご質問があるなら、私に教えてください。ありがとうございました!

+0

「ユーザーの名前はフィルタリングしますがUIではフィルタリングしません」と画像で説明してくださいあなたは「入力するときにフィルターにかけたくない」と言っていますか? – 3stud1ant3

+0

@ 3stud1ant3確かに。私が言ったことは、入力時に "textDidChange"メソッドで発生したフィルタリングを出力することです。しかし、コレクションビューはまったくフィルタリングされません。何も起こっていない場合と同じように続きます –

+0

あなたが示した2つの画像の違いを説明してください。あなたのアプリの流れを理解することができますか、それとも同じですか? – 3stud1ant3

答えて

1

OPとの議論の後、我々は解決策

を発見した

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() 
} 
を追加すると、これらの

最初の更新をあなたの検索バーの方法に従ってください。