2015-02-09 31 views
0

我遇到了searchDisplayController和自定義原型單元格的問題。我的故事板有一個帶有「Tulona」標識符的自定義原型單元格的表格視圖。故事板還有一個搜索顯示控制器。當搜索欄專注時,我想顯示一些搜索選項作爲基本單元格,並選擇一個選項後,我想用該自定義單元格重新加載表。我申請下面的代碼。選擇一個選項後不會完全刷新/重新加載後,除表格視圖之外,所有內容都是完美的。當我從顯示控制器的表格視圖中選擇一個選項時,主表格視圖(帶有「Tulona」單元格的故事板表格)不會出現。如果我從搜索欄點擊取消按鈕,主表視圖確實出現在自定義單元格中。請讓我知道爲什麼主表沒有從顯示控制器表視圖中選擇選項後重新加載?我是iOS開發新手。我很抱歉,如果這是真的很明顯或容易,我只是錯過了它。如果有的話,請指出正確的方向..謝謝。行選擇後不重新加載表視圖

class SecondViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate { 
    var brandCollection : [BrandListItem]? 
    var aliaseCollection : [BrandAliaseItem]? 
    var brandsToCompare = [Brand]() 
    var currentSearchDataArray = [String]() 

    @IBOutlet weak var searchBar: UISearchBar! 
    @IBOutlet weak var tableView: UITableView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     self.tableView.separatorStyle = UITableViewCellSeparatorStyle.None 

     //var nib = UINib(nibName: "SearchTableViewCell", bundle: nil) 
     //self.searchDisplayController!.searchResultsTableView.registerNib(nib, forCellReuseIdentifier: "SearchOptions") 

     let dataManager = DataManager.sharedInstance 
     self.brandCollection = dataManager.brandCollection 
     self.aliaseCollection = dataManager.aliaseCollection  
    } 

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

    func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String!) -> Bool{ 
     println(searchString) 

     if !searchString.isEmpty{ 
      //Clearing current search data array for new search key filtering 
      self.currentSearchDataArray.removeAll(keepCapacity: false) 

      //Add all brand matching to new search key from brand collection to the current search data array 
      for brand in self.brandCollection! { 
       if brand.name.lowercaseString.rangeOfString(searchString.lowercaseString) != nil { 
        self.currentSearchDataArray.append(brand.name) 
       } 
      } 

      //Add a brand matching to new search key from aliase collection to the current search data array by avoiding duplication 
      for aliase in self.aliaseCollection! { 
       if aliase.aliaseName.lowercaseString.rangeOfString(searchString.lowercaseString) != nil { 
        var brandName = self.isBrandExist(aliase.brandId) 
        if !brandName.isEmpty{ 
         if !contains(self.currentSearchDataArray, brandName){ 
          self.currentSearchDataArray.append(brandName) 
         } 
        } 
       } 
      } 
     } 

     return true 
    } 


    //Return brand name if brand id and aliase id is same, otherwise empty 
    func isBrandExist(id:Int)->String{ 
     for brand in self.brandCollection! { 
      if id == brand.id { 
       return brand.name 
      } 
     } 
     return "" 
    } 


    func searchBarCancelButtonClicked(searchBar: UISearchBar) { 
     searchBar.resignFirstResponder() 
    } 

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

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     if tableView == self.searchDisplayController!.searchResultsTableView { 
      return self.currentSearchDataArray.count 
     } else { 
      return self.brandsToCompare.count 
     } 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     //let cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier("SearchOptions") as UITableViewCell 
     let cell = UITableViewCell() 
     // Check to see whether the normal table or search results table is being displayed and set the Brand object from the appropriate array 
     if tableView == self.searchDisplayController!.searchResultsTableView { 
      let brandName = self.currentSearchDataArray[indexPath.row] as NSString 
      cell.textLabel?.text = brandName 
      //cell.tag = brand.id 
     } else { 
      let cell: TulonaCell = self.tableView.dequeueReusableCellWithIdentifier("Tulona", forIndexPath: indexPath) as TulonaCell 
      var brand = self.brandsToCompare[indexPath.row] as Brand 
      cell.setCustomCellForTulona(brand.name, rating: brand.rating, NoOfComment: brand.no_of_comments) 
     } 

     return cell 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     if tableView == self.searchDisplayController!.searchResultsTableView { 
      self.brandsToCompare.append(Brand(id: 100, rating: 5, no_of_comments: "110", name: "Wolf", aliases: [])) 
      self.tableView.reloadData() 
      self.searchDisplayController!.searchBar.resignFirstResponder() 
     }else{ 
      println("Table view should not reload") 
     } 
    } 
} 

回答

0

我已經通過將這些線didSelectRowAtIndexPath方法函數解決我的問題 -

dispatch_async(dispatch_get_main_queue()){ 
       self.tableView.reloadData() 
       self.searchDisplayController!.setActive(false, animated: true) 
       println("Table view should reload") 
      } 
+0

我沒有解決我的問題與那些代碼,但我的表視圖細胞總是充滿了相同的數據的數據數組self.brandsToCompare的第一個索引。我該如何解決這個問題?請幫助我。 – Nuibb 2015-02-10 04:19:19