user3882720 user3882720 - 3 months ago 12
MySQL Question

Search with UISearchBar through WebServer

What is the correct procedure to search with UISearchBar through a web server? The following code is what I have tried so far. It is working but not in the right way. I am not getting all the results as it has to be.

class PlacesViewController: UITableViewController, UISearchBarDelegate, UISearchControllerDelegate, UISearchDisplayDelegate {

@IBOutlet var searchController: UISearchBar!


var searchActive: Bool = false
var filtered = [PlacesData]()
var startSearching: Bool = false

var DataTable:[PlacesData] = []
var nameToPass: String!
var totalvisits: String!
var totallikes: String!

override func viewDidLoad() {

tableView.delegate = self
tableView.dataSource = self
searchController.delegate = self
definesPresentationContext = true
searchController.showsCancelButton = true

self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);

donwloadData()

}



func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchActive = true;
tableView.reloadData()

print("textdidbegin")
}

func searchBarTextDidEndEditing(searchBar: UISearchBar) {
searchActive = false;
print("textdidend")

}

func searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchActive = false;
searchBar.text = ""
searchBar.resignFirstResponder()
self.filtered.removeAll()
self.tableView.reloadData()
print("textdidcancel")

}

func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchActive = false;
print("textdidsearch")

}

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
print("search")

self.filtered.removeAll()

tableView.reloadData()

Alamofire
.request(.POST, "mysite.com/search.php", parameters: ["type":"Stores", "word": searchText])
.responseJSON { response in

if(response.result.description == "SUCCESS"){
let json = JSON(response.result.value!)
let arrayjson = json.arrayValue
let jsondict = arrayjson[0].dictionaryValue
let firstarr = jsondict["words"]?.arrayValue
for item in firstarr!{

let name = item["name"].stringValue
let long = item["long"].stringValue
let lat = item["lat"].stringValue

print(name)
self.filtered.append(PlacesData(name: name, long: long, lat: lat))
}
}

self.tableView.reloadData()
}

}


func donwloadData(){
Alamofire
.request(.GET, "mysite.com/showplaces.php")
.responseJSON { response in

print(response.result)
if(response.result.description == "SUCCESS"){

let json = JSON(response.result.value!)


let arrayjson = json.arrayValue

let jsondict = arrayjson[0].dictionaryValue

let firstarr = jsondict["words"]?.arrayValue

for item in firstarr!{

let name = item["name"].stringValue
let long = item["long"].stringValue
let lat = item["lat"].stringValue

print(name)
self.DataTable.append(PlacesData(name: name, long: long, lat: lat))
}


self.tableView.reloadData()
}
}
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (searchActive == true) {
return filtered.count + 1
}else{
return DataTable.count
}
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! PlacesCel

let places: PlacesData


if (searchActive == true) {
print("search IS active")
if(indexPath.row == filtered.count){

cell.titleLabel.text = "Add Place"

}else{

places = filtered[indexPath.row]
cell.textLabel?.text = places.name
}
} else {
print("search IS NOT active")

places = DataTable[indexPath.row]
cell.textLabel?.text = places.name
}

return cell
}


I could not find a proper tutorial to find the correct way of doing that. Apparently this is not working properly and whatever I tried it is not working. Any solution or answer would be appreciated. Thank you.

Answer

After all the problem was that I was not canceling my requests and every time I was typing a character in the searchbar a new request was added and all were added together at the end after every request was finished. The following code gave me the solution.

    if((self.request) != nil){
    request?.cancel()
    }

    self.request = Alamofire()
Comments