2016-08-26 1 views
0

웹 서버를 통해 UISearchBar를 검색하는 올바른 절차는 무엇입니까? 다음 코드는 지금까지 시도한 코드입니다. 그것은 효과가 있지만 올바른 방법은 아닙니다. 나는 모든 결과를 얻지 못하고 있습니다.웹 서버를 통해 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 
} 

정확한 방법을 찾지 못했습니다. 분명히 이것은 제대로 작동하지 않으며 내가 시도한 것은 작동하지 않습니다. 어떤 해결책이나 대답을 주시면 감사하겠습니다. 고맙습니다.

답변

0

모든 문제는 내가 요청을 취소하지 않았으며 검색 창에 문자를 입력 할 때마다 모든 요청이 완료된 후에 새로운 요청이 추가되었으며 마지막에 모두 모두 추가되었습니다. 다음 코드는 나에게 해결책을 주었다.

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

    self.request = Alamofire()