2017-08-29 180 views
0

好吧,我希望這將是有意義:搜索欄有困難過濾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 ?? "" 
} 

}

讓我知道,如果你有了解該問題的任何問題。謝謝!

+0

請解釋「它會過濾用戶名,但不在用戶界面上」和圖像喲你說「打字時不想過濾」? – 3stud1ant3

+0

@ 3stud1ant3當然。我試圖說的是,它打印輸入時發生在「textDidChange」方法中發生的過濾。但收集視圖根本不會過濾。它保持不變,就像沒有任何事情發生一樣 –

+0

也請解釋你展示的兩幅圖像之間的差異,所以我可以理解你的應用程序的流程還是它們是相同的? – 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() 
}