Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Searching TableView can't select row

While searching a tableView, every time I try to select a row it just takes me back to the unsearched tableView. What am I missing? the segue works perfectly when not filtering through the table. The ability to select a row just disapears while the searchBar is activated.

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

}
like image 391
Brandon512 Avatar asked Jul 29 '16 04:07

Brandon512


2 Answers

Figured it out after playing around. Apparently adding the below code corrects the problem.

searchController?.dimsBackgroundDuringPresentation = false
like image 55
Brandon512 Avatar answered Oct 21 '22 02:10

Brandon512


swift 'dimsBackgroundDuringPresentation' was deprecated in iOS 12.0 Use the obscuresBackgroundDuringPresentation property instead.

searchController?.obscuresBackgroundDuringPresentation = false
like image 41
Markinson Avatar answered Oct 21 '22 04:10

Markinson