搜索TableView无法选择行

时间:2016-07-29 04:22:08

标签: ios swift uitableview swift2

在搜索tableView时,每次尝试选择一行时,它都会将我带回到未搜索的tableView。我错过了什么?当不通过表过滤时,segue完美地工作。激活searchBar时,选择行的能力就会消失。

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)
    }

}

2 个答案:

答案 0 :(得分:10)

玩完后想出来。显然添加以下代码可以解决问题。

searchController?.dimsBackgroundDuringPresentation = false

答案 1 :(得分:2)

在iOS 12.0中不建议使用swift'dimsBackgroundDuringPresentation'属性,而应使用obscuresBackgroundDuringPresentation属性。

searchController?.obscuresBackgroundDuringPresentation = false