通过WebServer使用UISearchBar进行搜索

时间:2016-08-26 11:31:05

标签: mysql swift webserver uisearchbar

通过Web服务器使用UISearchBar进行搜索的正确步骤是什么?以下代码是我到目前为止所尝试的。它工作但不是正确的方式。我没有得到所有结果。

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
}

我找不到合适的教程来找到正确的方法。显然这不能正常工作,无论我尝试过什么都行不通。任何解决方案或答案将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

毕竟问题是我没有取消我的请求,每次我在搜索栏中输入一个字符时,都会添加一个新请求,并在每个请求完成后将所有请求添加到一起。以下代码为我提供了解决方案。

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

    self.request = Alamofire()