0
當我用以下結構檢索跑,我能得到的結果,但我有幾個問題:搜索無法正常使用UISearchController
- 當我搜索一個人,我可以看到在底部有導致輸出區域打印命令但它不顯示在屏幕上。如果我刪除一個單詞,它會顯示結果!例如,如果我鍵入Michael,則不會顯示任何結果,但是如果我在Michael單詞的末尾刪除字母L,則會正確顯示結果。
- 有時搜索欄會作爲第一響應者,有時不會。我需要點擊兩次搜索。
- 如果我在搜索詞後鍵入一個空格,應用程序會崩潰,並顯示「致命錯誤:數組索引超出範圍」錯誤。
我的代碼如下:
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if shouldShowSearchResults {
return filteredArray.count
}
else {
return homesearchResults.count
}
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
filteredTableData.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
if searchController.searchBar.text?.characters.count > 2
{
goSearch(searchController.searchBar.text!)
}
let array = (homesearchResults as NSArray).filteredArrayUsingPredicate(searchPredicate)
filteredArray = array as! [String]
self.homeTableView.reloadData() }
func configureSearchController() {
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search"
searchController.searchBar.delegate = self
searchController.searchBar.sizeToFit()
let textFieldInsideSearchBar = searchController.searchBar.valueForKey("searchField") as! UITextField
textFieldInsideSearchBar.font = UIFont(name: "Bauhaus", size: 19)
searchController.searchBar.setImage(UIImage(named: "searchicon"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal);
homeTableView.tableHeaderView = searchController.searchBar}
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchController.searchBar.becomeFirstResponder()}
func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
print ("searchBarSearchButton Clicked")
return true
}
func goSearch(searchWord: String)
{
homeSearchBar.resignFirstResponder()
let myUrl = NSURL(string: "http://bla.net/home.php")
let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "POST";
let defaults = NSUserDefaults.standardUserDefaults()
let user_id = defaults.integerForKey("dbID") as Int
let postString = "searchWord=\(searchWord)&userId=\(user_id)";
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);
let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
data, response, error in
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
self.homesearchResults.removeAll(keepCapacity: false)
self.homeimageResults.removeAll(keepCapacity: false)
self.homeidResults.removeAll(keepCapacity: false)
self.homeTableView.reloadData()
if let parseJSON = json {
print(parseJSON)
if let friends = parseJSON["friends"] as? [AnyObject]
{
for friendObj in friends
{
let fullName = (friendObj["name"] as! AnyObject)
self.homesearchResults.append(fullName as! String)
let friendRequestImage = (friendObj["thumb"] as! AnyObject)
self.homeimageResults.append(friendRequestImage as! String)
let NotID = (friendObj["id"] as! AnyObject)
self.homeidResults.append(NotID as! String)
}
self.homeTableView.reloadData()
} else if(parseJSON["message"] != nil)
{
let errorMessage = parseJSON["message"] as? String
if(errorMessage != nil)
{
self.displayAlertMessage(errorMessage!)
}
}
}}
catch {
print("json error: \(error)")
}
}
task.resume()
}