2016-11-20 9 views
0

私はこれを数時間かけて解決しようとしました。私はIOSの初心者で、検索バーでバケットリストを作成したいと考えています。コードはコンパイルには問題ありませんが、検索バーに入力すると検索結果は表示されません。私はどこに問題があるのか​​分からない。検索バーが機能しないのはなぜですか?

btw:私は2人の容疑者を持っています: 1.ステータスを印刷しようとしましたが、searchBarは決してアクティブではないようです。それを開く方法? 2. var missionのデータ のタイプはMissionと何か関係がありますか?それは私が慣れていないものです。あなたのcellForRowAtIndexPath方法に問題がある、あなたが共有しているプロジェクトから

import UIKit 

class BucketListViewController:UITableViewController, MissionDetailsViewControllerDelegate, MissionEditViewControllerDelegate, UISearchBarDelegate{ 

    var searchController = UISearchController(searchResultsController: nil) 
    @IBOutlet weak var searchBar: UISearchBar! 

    var searchActive : Bool = false 
    var filtered:[String] = [] 
    var missions:[String] = ["Sky diving", "Live in Hawaii"] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     searchBar.delegate = self 
     self.searchDisplayController!.searchResultsTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MyCell") 
    } 

    func searchBarTextDidBeginEditing(searchBar: UISearchBar) { 
     searchActive = true; 
    } 

    func searchBarTextDidEndEditing(searchBar: UISearchBar) { 
     searchActive = false; 
    } 

    func searchBarCancelButtonClicked(searchBar: UISearchBar) { 
     searchActive = false; 
    } 

    func searchBarSearchButtonClicked(searchBar: UISearchBar) { 
     searchActive = false; 
    } 

    func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { 

     filtered = missions.filter({ (text) -> Bool in 
      let tmp: NSString = text 
      let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch) 
      return range.location != NSNotFound 
     }) 
     if(filtered.count == 0){ 
      searchActive = false; 
     } else { 
      searchActive = true; 
     } 
     self.tableView.reloadData() 
    } 


    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     // dequeue the cell from our storyboard 
     let cell = tableView.dequeueReusableCellWithIdentifier("MyCell")! 
     if(searchActive){ 
      print("search active!") 
      cell.textLabel?.text = filtered[indexPath.row] 
     } else { 
      cell.textLabel?.text = missions[indexPath.row] 
     } 
     return cell 
    } 


    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if(searchActive){ 
      return filtered.count 
     } else { 
      return missions.count 
     } 
    } 



    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     if segue.identifier == "AddNewMission" { 
      let navigationController = segue.destinationViewController as! UINavigationController 
      let controller = navigationController.topViewController as! MissionDetailsViewController 
      controller.delegate = self 
     } 
     if segue.identifier == "EditMission" { 
      let navigationController = segue.destinationViewController as! UINavigationController 
      let controller = navigationController.topViewController as! MissionEditViewController 
      controller.delegate = self 
     } 
    } 


    func missionDetailsViewController(controller: MissionDetailsViewController, didFinishAddingMission mission: String) { 
     dismissViewControllerAnimated(true, completion: nil) 
     missions.append(mission) 
     tableView.reloadData()} 

    func missionEditViewController(controller: MissionEditViewController, didFinishEditingMission mission: String, atIndexPath indexPath: Int) { 
     dismissViewControllerAnimated(true, completion: nil) 
     missions[indexPath] = mission 
     tableView.reloadData() 

    } 
} 

PICTURE ON WORKING ERROR OF SEARCH BAR

+0

おそらく、あなたの検索バーは、ストーリーボードにIBOutletに適切にフックアップされていませんか? –

+0

私はそれを@IBOutlet弱いvarのsearchBar:UISearchBarでフックアップしたと思います!どうすれば確認できますか? – marshall

+0

@marshall 'textDidChange'メソッドが呼び出されているのを確認しましたか? – Rajat

答えて

0

次のコードを参照してください。

cell.textLabel?.text = filtered[indexPath.row].details 

cell.textLabel?.text = filtered[indexPath.row] as? String 

を交換し、それが正常に動作します。

あなたの完全なコードは次のようになります。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    // dequeue the cell from our storyboard 
    let cell = tableView.dequeueReusableCellWithIdentifier("MissionCell")! 
    // if the cell has a text label, set it to the model that is corresponding to the row in array 
    if(searchActive){ 
     print("search active!") 
     cell.textLabel?.text = filtered[indexPath.row].details 
    } else { 
     cell.textLabel?.text = missions[indexPath.row].details 
    } 
    return cell 
} 
+0

それは動作します。ありがとう! – marshall

+1

答えがあれば受け入れてください:) –

関連する問題