Зачем отправлять мои результаты поиска в виде таблицы? - PullRequest
0 голосов
/ 09 января 2019

Я пытаюсь выполнить поиск других пользователей только тогда, когда пользователь набрал хотя бы 2 слова, и только потом начинаю поиск в базе данных (потому что я не хочу сканировать всю базу данных для пользователей). У меня были некоторые проблемы с двухбуквенным поиском, но я думаю, что получил код (спасибо пользователю Jay).

Однако, когда я запускаю его в симуляторе, консоль выводит имя, но в табличном представлении ничего не отображается? (пусто).

Вы знаете, что я сделал не так?

Это мой код:

class FollowUsersTableViewController: UIViewController {

    @IBOutlet var tableView: UITableView!

    private var viewIsHiddenObserver: NSKeyValueObservation?
    let searchController = UISearchController(searchResultsController: nil)
    var usersArray = [UserModel]()
    var filteredUsers = [UserModel]()
    var loggedInUser: User?
    //
    var databaseRef = Database.database().reference()
    //usikker på den koden over

    override func viewDidLoad() {

        super.viewDidLoad()

        searchController.searchBar.delegate = self


        //large title
        self.title = "Discover"
        if #available(iOS 11.0, *) {
            self.navigationController?.navigationBar.prefersLargeTitles = true
        } else {
            // Fallback on earlier versions
        }

        self.tableView?.delegate = self
        self.tableView?.dataSource = self
        searchController.searchResultsUpdater = self
        searchController.dimsBackgroundDuringPresentation = false
        self.searchController.delegate = self;



        definesPresentationContext = true
        tableView.tableHeaderView = searchController.searchBar



    }

    func searchUsers(text: String) {
        if text.count >= 2 {
            self.usersArray = [] //clear the array each time
            let endingText = text + "\u{f8ff}"
            databaseRef.child("profile").queryOrdered(byChild: "username")
                .queryStarting(atValue: text)
                .queryEnding(atValue: endingText)
                .observeSingleEvent(of: .value, with: { snapshot in

                    for child in snapshot.children {
                        let childSnap = child as! DataSnapshot
                        print(childSnap)
                        let userObj =  Mapper<UserModel>().map(JSONObject: childSnap.value!)
                        userObj?.uid = childSnap.key
                        if childSnap.key != self.loggedInUser?.uid { //ignore this user
                            self.usersArray.append(userObj!)

                        }
                    }
                    self.tableView.reloadData()
                })
        }
    } //may need an else statement here to clear the array when there is no text


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let dest = segue.destination as! UserProfileViewController
        let obj = sender as! UserModel
        let dict = ["uid": obj.uid!, "username": obj.username!, "photoURL": obj.photoURL, "bio": obj.bio]
        dest.selectedUser = dict as [String : Any]
    }





}

// MARK: - tableview methods
extension FollowUsersTableViewController: UITableViewDataSource, UITableViewDelegate {



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return searchController.searchBar.text!.count >= 2 ? filteredUsers.count : 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! FollowTableViewCell

        let user = filteredUsers[indexPath.row]

        cell.title?.text = user.username
        if let url = URL(string: user.photoURL ?? "") {
            cell.userImage?.sd_setImage(with: url, placeholderImage: #imageLiteral(resourceName: "user_male"), options: .progressiveDownload, completed: nil)
            cell.userImage.sd_setIndicatorStyle(.gray)
            cell.userImage.sd_showActivityIndicatorView()
        }

        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.performSegue(withIdentifier: "user", sender: self.filteredUsers[indexPath.row])
    }



}

// MARK: - search methods
extension FollowUsersTableViewController:UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate {



    func updateSearchResults(for searchController: UISearchController) {
        searchController.searchResultsController?.view.isHidden = false

        self.searchUsers(text: self.searchController.searchBar.text!)

        filterContent(searchText: self.searchController.searchBar.text!)

        self.tableView.reloadData()
    }

    func filterContent(searchText:String){

        if searchText.count >= 2{


            self.filteredUsers = self.usersArray.filter{ user in
                return(user.username!.lowercased().contains(searchText.lowercased()))
            }
        }
    }


}

Ответы [ 2 ]

0 голосов
/ 10 января 2019

Этот код очень близок, и, как показывает другой ответ, у вас есть два массива.

Но давайте действительно упростим проблему: вам нужен только один массив, поскольку вы фильтруете Firebase, а не массив.

Другими словами, когда пользователь вводит данные в searchField, вы запрашиваете у Firebase результаты и помещаете их в массив. Это тот же массив, который вы должны использовать в качестве источника данных для вашего tableView.

Когда ваш код сидит, вы фильтруете Firebase, а затем фильтруете те результаты снова , которые не нужны.

Итак, на высоком уровне предполагается, что у нас в базе данных четыре пользователя

Larry
Moe
Monroe
Curly

пользователь вводит 'Mo' в поле поиска, которое вызывает выполнение вашего запроса Fire. Он вернет два результата:

Mo
Monroe

, который затем заполняется в массив - мы назовем его userResultsArray.

1021 * тогда *

tableView.reloadData()

, который затем вызывает методы делегата tableView

func tableView(_ tableView: UITableView, numberOfRowsInSection
   return userResultsArray.count

и

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
   get the user from userResultsArray
   return the user name

Как показано, вам нужен только один массив для хранения запрашиваемых (отфильтрованных) результатов из Firebase.

0 голосов
/ 09 января 2019

У вас есть 2 массива usersArray и filteredUsers

if childSnap.key != self.loggedInUser?.uid { //ignore this user
       self.usersArray.append(userObj!)
}
...
 self.tableView.reloadData()

, поэтому приведенная выше перезагрузка не имеет никакого эффекта, поскольку вы всегда используете

let user = filteredUsers[indexPath.row]

в cellForRowAt, в таких случаях вы должны иметь переменную типа

var isSearching = false

и измените его при поиске во всех методах делегата и источника данных.

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return isSearching ? filteredUsers.count : usersArray.count
 }
 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! FollowTableViewCell

    let user = isSearching ? filteredUsers[indexPath.row] : usersArray[indexPath.row] 
  .....
 }

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    isSearching = true
}

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
    isSearching = false
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    isSearching = false
}

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    isSearching = false
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    filteredUsers = usersArray.filter { /* do filter */ }

    if(filteredUsers.count == 0){
        isSearching = false
    } else {
        isSearching = true
    }
    self.tableView.reloadData()
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.performSegue(withIdentifier: "user", sender: isSearching ? self.filteredUsers[indexPath.row] : self.usersArray[indexPath.row])
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...