Reputation: 1423
I have a search bar where I search for users. When I first search for a user, it displays the correct result. But when I move to the detailed view (to the user's profile), tap any button there, and move back, the value triplicates. It doesn't affect the result when I move, don't tap on any button, and move back. Here are the code snippets that I use:
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchBar.delegate = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search people (by username)"
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
requestManager.delegate = self
}
func updateSearchResults(_ sender: RequestManager) {
tableView.reloadData()
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
requestManager.resetSearch()
requestManager.searchUser(validatedText)
print("Validated text: \(validatedText)")
}
func searchUser(_ searchText: String) {
refUsers.observe(.value, with: { (snapshot) in
let snapshotDictionary: [String: AnyObject] = snapshot.value as! [String : AnyObject]
for (userKey, _) in snapshotDictionary {
if let usernameValue = snapshot.childSnapshot(forPath: userKey).childSnapshot(forPath: "username").value as? String
{
if usernameValue.lowercased().contains(searchText.lowercased()) {
let userData = JSON(snapshot.childSnapshot(forPath: userKey).value!)
self.searchUsers.append(userData)
}
if self.delegate != nil {
self.delegate?.updateSearchResults(self)
}
}
else {
print("Users could not be found.")
}
}
}) { (error) in
print(error.localizedDescription)
}
}
What seems to be calling the result thrice when I move back from the detail view? None of the functions here are in the detail view.
Upvotes: 2
Views: 3028
Reputation: 4373
Simply remove objects from the array before adding more.
self.searchUsers.removeAll()
self.searchUsers.append(userData)
So just add that piece of code and you will be good to go.
Upvotes: 4
Reputation: 5053
You can try bellow like . On search
//Original array
var arrRes = [[String:AnyObject]]()
//Search Array
var searchArrRes = [[String:AnyObject]]()
var searching:Bool = false
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchArrRes = self.arrRes.filter({(($0["branchName"] as! String).localizedCaseInsensitiveContains(searchText))})
if(searchArrRes.count == 0){
searching = false
}else{
searching = true
}
self.tableView.reloadData();
}
Just like give the condition to table view method
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if( searching == true){
return searchArrRes.count
}else{
return arrRes.count
}
}
Set value to table cell like bellow
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:BranchLocationCell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomCell
if( searching == true){
cell.tv_branch_name.text = searchArrRes[indexPath.row]["branchName"] as! String
}else{
cell.tv_branch_name.text = arrRes[indexPath.row]["branchName"] as! String
}
return cell;
}
Upvotes: 0
Reputation: 14995
Looks like in your same initiliazed array you are appending the data. So try in this way Whenever you tapped on button re-initiliazed your array or check the duplicate entry inside your array and accordingly append the data. Either approach you can follow.
Upvotes: 0