2016-07-29 52 views
2

在搜索tableView时,每次尝试选择一行时,它都会将我带回未检索的tableView。我错过了什么?当不通过表格进行过滤时,segue可以很好地工作。在searchBar被激活时,选择行的功能将消失。搜索TableView无法选择行

import UIKit 
import Foundation 

class BenchmarkWODViewController: UITableViewController, UISearchResultsUpdating { 

    var WodList = [WOD]() 
    var FilteredWodList = [WOD]() 
    var Keyword = "" 
    var searchController : UISearchController? 
    var index = Int() 

    @IBAction func backButton(sender: AnyObject) { 
     self.navigationController?.popViewControllerAnimated(true) 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     for wodData in BenchmarkWODs.library { 
      let wod = WOD(dictionary: wodData) 
      WodList.append(wod) 
    } 

    // Search Bar 
     self.searchController = UISearchController(searchResultsController: nil) 
     self.searchController?.searchBar.autocapitalizationType = .None 
     self.tableView.tableHeaderView = self.searchController?.searchBar 
     self.searchController?.searchResultsUpdater = self 
     self.Keyword = "" 
     definesPresentationContext = true 

     self.filterByName() 
    } 


    func filterByName(){ 
     self.FilteredWodList = self.WodList.filter({ (wod: WOD) -> Bool in 
      if self.Keyword.characters.count == 0 { 
      return true 
     } 

     if (wod.name?.lowercaseString.rangeOfString(self.Keyword.lowercaseString)) != nil { 
      return true 
     } 
     return false 
    }) 
     self.tableView.reloadData() 
    } 


    // Search Bar Function 
    func updateSearchResultsForSearchController(searchController:  UISearchController) { 
     Keyword = searchController.searchBar.text! 
     self.filterByName() 
    } 



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

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return self.FilteredWodList.count 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     let cell = tableView.dequeueReusableCellWithIdentifier("BenchmarkCell", forIndexPath: indexPath) as UITableViewCell 
     let wod = self.FilteredWodList[indexPath.row] 

     if let wodName = wod.name { 
     cell.textLabel?.text = wodName 
    } 
     return cell 
    } 



    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    self.filterByName() 
     self.performSegueWithIdentifier("showBenchmarkDetail", sender: nil) 
    } 

} 
+0

尝试关闭didselectrowatindexpath方法中的搜索控制器。 –

+0

为什么在'didSelectRow'中调用'filterByName'? – Losiowaty

+0

你有搜索栏代理功能 –

回答

3

玩过之后就想出来了。显然添加下面的代码可以解决问题。

searchController?.dimsBackgroundDuringPresentation = false